Question

This is a C++ question we do not use namespace std at the beginning of the...

This is a C++ question

we do not use namespace std at the beginning of the program

9. Arrays: Functions and Reading From File

Although this code is a little long, the strategy is straightforward and it will really help you practice functions. I have provided a large amount of detail to help you out. In a nutshell, you're going to read some numbers from a file (into an array) and then ask the user for a number that will be used to modify the array values via multiplication. You will need THREE functions:

  • Function #1 - Read the numbers from easy-numbers.txt into an array. This file has been included in your Mimir starter code. It is simply the numbers 1 thru 10.
  • Function #2 - Modify the array based on the user's multiplier. Example: If the user types 2, you will multiply each number in the array by 2. Instead of 1,2,3... the array will now hold 2,4,6...
  • Function #3 - Print all values in the array

So what does main() have? It will call the function that reads the numbers from the file, output the introductory message, and then ask the user for a number two times. After the user provides a number, it must call the appropriate functions to manipulate the array and output the result of that manipulation.

Output:

This program will alter a list of numbers based on your two desired multipliers.
Enter the first multiplier: [user types: 3]

3 6 9 12 15 18 21 24 27 30
Enter the second multiplier: [user types: 2]

6 12 18 24 30 36 42 48 54 60

Notes and Hints:

1) Yes, the user's input must be passed from main() to one of the functions in order for it to work

2) Although I only asked for three functions that focus on arrays, you are welcome to place the user's input in its own function. Use those Chp 6 skills!

The file called easy-numbers.txt contains

1
2
3
4
5
6
7
8
9
10

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>
#include<fstream>
#include <bits/stdc++.h>

using namespace std;

int readDataFromFile(string fileName);
void multiplyArray(int arr[], int size, int multiplier);
void printArray(int arr[], int size);

int data[INT_MAX]; //declare an array of maximum size to assign data from file

int main() {
   string filePath = "easy-numbers.txt"; //This the path of file to read data from
   int size,multiplier;
  
   size = readDataFromFile(filePath);  
  
   if(size==0) {
       return 0; //if no data found from the file then exit
   }
  
   cout<<"Enter the first multiplier: ";
   cin>>multiplier; //first user input
   multiplyArray(data, size, multiplier);
   printArray(data, size);
  
   cout<<"\nEnter the second multiplier: ";
   cin>>multiplier; //second user input  
   multiplyArray(data, size, multiplier);
   printArray(data, size);
}

//This method takes filepath as argument
//read data from the file and assign data to the array
//return size of the array
int readDataFromFile(string filePath) {
   ifstream infile;
   infile.open(filePath.c_str()); //oprn file to read
  
   if(!infile.is_open()) { //return zero if file not found
       cout<<"Unable to read the file: "<<filePath;
       return 0;
   }
   int a;
   int index = 0;
   while(infile >> a) { //read data from file and assign value to a
       data[index] = a; //assign read value to array
       index++;
   }
   infile.close(); //close the file
   return index; //returns the number of elements added to the array
}

//This methid takes a array, size of array, multiplers value as argument
//multiplies each element of arrr=ay by multiplier value
void multiplyArray(int arr[], int size, int multiplier) {
   int i;
   for(i=0; i<size; i++) { //multiple each element of array by the "multiplier" value
       arr[i]*=multiplier;
   }
}

//This method takes array, size of the array as input
//print content of the array
void printArray(int arr[], int size) {
   int i;
   for(i=0;i<size;i++) { //read the array and print its value
       cout<<data[i]<<" ";
   }
}

Add a comment
Know the answer?
Add Answer to:
This is a C++ question we do not use namespace std at the beginning of the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This is a c++ question we are not using namespace std at the top Subtraction +...

    This is a c++ question we are not using namespace std at the top Subtraction + Decision and Loop This is a twist on the previous exercise that will help you review loops and decision structures. You will again ask the user to enter two numbers. However, you will ALWAYS subtract the smaller number from the larger number to ensure that you never get a negative number for an answer. You do this by checking the numbers and switching them...

  • Thank you! /*Lab 8 : Practicing functions and arrays Purpose: */ #include<iostream> #include<fstream> using namespace std;...

    Thank you! /*Lab 8 : Practicing functions and arrays Purpose: */ #include<iostream> #include<fstream> using namespace std; int read_function(int array[], int, int); int main() { int array[300], numba; read_function(array[300]); cout << "Enter a whole number between 2-20: " << endl; cin >> numba; read_function(numba); return 0; } int read_funtion (int arr[300], int num, int Values) { int sum=0; ifstream array_file; array_file.open("Lab8.dat"); for(int i=0; i < 300; i++) {   array_file >> arr[i];   cout << arr[i];   sum += i; } cout << sum;...

  • Please help code in c++ and use the answers you get from question 1 and 2...

    Please help code in c++ and use the answers you get from question 1 and 2 into the 3rd answer! Question 1 is first, question 2 is in the middle, question 3 is last Input Array (10 pts) te a function only (no main) that takes an array of doubles as a parameter as well as another integer rray. Create a for loop that asks the user to input any real number between-100 and r each element of the array....

  • This is a c++ question note: not using  namespace std; at the beginning of the program Writing...

    This is a c++ question note: not using  namespace std; at the beginning of the program Writing Data to a File This program will write a series of letters, starting with 'A', to an external file (letters.txt). The user will decide how many letters in total get saved to the file. ** IMPORTANT: The test cases will evaluate your code against .txt files that I uploaded. You do NOT have to upload your own txt files. Input: Including 'A', how many...

  • Implement the following in c++ (use "iostream" and "nsmespace std" please.)

    Implement the following: a. A template class named MyArray. 1) MyArray is a dynamic partially filled array for primitive types. 2) data members: - a pointer for the array - any associated variables needed to manage the array. 3) Constructor must insure that specified capacity is possible. Exit the program if an illegal value is specified. 4) “The Big Three” are required to insure deep copy. 5) Private grow function is used to automatically increase the size of the array...

  • Write one single program that does all of the following tasks, in order. You should practice...

    Write one single program that does all of the following tasks, in order. You should practice proper Top Down Design and Procedural Abstraction techniques unless otherwise noted. -Write a program with the following functions, with the correct number of arguments for each function. -void fillUpArray ( argument1, argument2) This function should read in a text file called "villagers.txt" (You will create your own). "villagers.txt" is a text file of numbers in ascending order, ranging anywhere from 1 to 100, nonrepeating....

  • c++. please show screenshots of output This project will continue to familiarize the student with using...

    c++. please show screenshots of output This project will continue to familiarize the student with using arrays. Store all the function proto-types in project11_funch and store all the functions in project11_func.cpp. Make sure you have proper header comments in each.h,.cpp file. When you run your program, your sample output should show your name. Your report should include the following: 1) project11_func.h 2) project11_func.cpp 3) project11.cpp 4) sample output Write the following functions: a) void fill_array_with_random_nums(int array(), int N): Fill array...

  • C++ no std: cout but cout<<" ". also nothing ".h": in the header files: Also if...

    C++ no std: cout but cout<<" ". also nothing ".h": in the header files: Also if statements should be used for the file portion. (filein.eof is not aloud) generate integer random numbers and place them in a file. Each number on a single line. The numbers should range between 1-200 and the program should generate between 100-150 numbers. This means each time the program is executed, a file is created containing between 100-150 numbers with values between 1-200. Make sure...

  • To use the digits function, enter 1 To use the average function, enter 2 To use the perfect sum f...

    use matlab To use the digits function, enter 1 To use the average function, enter 2 To use the perfect sum function, enter3 To exit the program, enter 4 Please select a number = 6 Please re-select again: 2 please enter the first number 3 please enter the second number: 6 please enter the third number: 3 The average equals to: 4 Write a function, called digits function that is able to calculate the number of digits and the summation...

  • C++ Read and do as instructed on please (C++Program *** use only condition statements, loops, functions,...

    C++ Read and do as instructed on please (C++Program *** use only condition statements, loops, functions, files, and arrays. Do NOT use material such as classes. Make sure to add comments** You are to write an ATM Program. The ATM should allow the user to do the following: 1. Create account 2. Log in 3. Exit When they press 1, they are asked for first name (capital first letter). Then it should ask for password. The computer should give the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT