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
  • 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;...

  • 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...

  • 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....

  • 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...

  • 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...

  • 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....

  • Kindly solve this using C PROGRAMMING ONLY. 4. Write a program marks.c which consists of a...

    Kindly solve this using C PROGRAMMING ONLY. 4. Write a program marks.c which consists of a main function and three other functions called. readmarks , changemarks (, and writemarks() The program begins by calling the function readmarks () to read the input file marksin. txt which consists of a series of lines containing a student ID number (an integer) and a numeric mark (a float). Once the ID numbers and marks have been read into arrays (by readmarks () the...

  • In this assignment, you will revisit reading data from a file, and use that data as...

    In this assignment, you will revisit reading data from a file, and use that data as arguments (parameters) for a number of functions you will write. You will need to: Write and test a function square_each(nums) Where nums is a (Python) list of numbers. It modifies the list nums by squaring each entry and replacing its original value. You must modify the parameter, return will not be allowed! Write and test a function sum_list(nums) Where nums is a (Python) list...

  • Write a C++ program that asks user number of students in a class and their names....

    Write a C++ program that asks user number of students in a class and their names. Number of students are limited to 100 maximum. Then, it will ask for 3 test scores of each student. The program will calculate the average of test scores for each student and display with their names. Then, it will sort the averages in descending order and display the sorted list with students’ names and ranking. Follow the Steps Below Save the project as A4_StudentRanking_yourname....

  • Using Java In this assignment we are working with arrays. You have to ask the user...

    Using Java In this assignment we are working with arrays. You have to ask the user to enter the size of the integer array to be declared and then initialize this array randomly. Then use a menu to select from the following Modify the elements of the array. At any point in the program, if you select this option you are modifying the elements of the array randomly. Print the items in the array, one to a line with their...

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
ADVERTISEMENT