Question

Goals: Practicing arrays and functions Create a program that will read values from a file called Lab8.dat (posted on Canvas) and store it into an array using a function other than main. The number of values in the file is less than 300 and all the values are whole numbers. The actual number of values stored in the file should be returned to the function call Your program should contain another function that accepts the array, the number of values in the array, and a whole number. This function should use a loop to count how many values in the array are evenly divisible by the whole number and return this count to the function call. The loop should not contain more than 3 arithmetic operators and count as arithmetic operators, but does not) The main function should call the function to read the data from the file. Next, ask the user to enter a whole number between 2 and 20 (you may assume the user enters a correct value). This whole number is the value to divide each element in the array. Next, call the function to determine how many values in the file are evenly divisible by the entered whole number and output the returned count. The message should be something like 35 of the 189 values in the file were evenly divisible by 19 with the underlined values replaced by the your calculated values and the users input. Do not use any concepts beyond Chapter 7 of your textbook. Remember that in addition to the introductory comments for the program your code should contain comments for each function other than main that states the purpose of the function, input to the function (both input from the function call and interactive input), output from the function (both information sent back to the function call and interactive output and processing performed in the function. Follow the Assignment Guidelines posted on Canvas.

LAB8.DAT IS LISTED BELOW

154    -6   279   215   404   391  -166   199  -302  -469   244     0   -20   405   110   118   359   305    77  -317  -260   387  -471   -10  -332   479   213     0   -29  -440   182  -458  -429    22  -403   318   318   222  -350   160    19   473   149   300   -46   -68   325  -417  -367  -327  -109   331   303  -440  -101    27   -83   157   128  -208   -68  -485   484  -333

-394  -128  -302   -10  -161   452   420  -447   238  -231   -77    48   443   -82   483  -199   201   166    39   198   167  -322  -372   499  -329  -467    61   382   169  -310  -131   -39   482  -344   356   145  -124  -309   -72   -18  -379    90  -274  -115    83  -248  -210   117  -235   324   483   230  -156    84  -392   406   380   318  -239    94  -477   -75  -187  -339

-321   -77  -406    99   -29   196   200   139  -466  -431  -180    31   154   -92   320   218   469    31  -175  -394   111   279   -77  -409  -234  -346  -219   -60    27   -43   375    18   444   138   458  -259   176  -211   172   195  -432  -245  -276   168   344  -156   281   175  -493   102  -113   416  -499   -38   -76   -39   270  -178   285   -29  -464  -324   222   -27

-347  -159   107  -308   238  -257   417  -231   266  -311  -213  -409    76   183    47   -74   144   148   179   136   445  -291   209  -264  -381   107   -50   -41   162   270  -150   162   -84   342   333  -244   113    82    41   370  -235  -182  -381   440   146   -21   139    45   147    44   221    22   494  -281  -394  -390  -436   -95   -52  -134   264   128   272   433

473  -308  -361   196  -406    25    30   361   -15  -107   171   241    20  -152  -350

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

HI, Please find my implementation.

Please let me know in case of any issue.

#include <iostream>
#include <fstream>
using namespace std;

int readData(int arr[]){

   int count = 0;
// opening file
ifstream inFile("LAB8.DAT");

if(inFile.fail()){
   cout<<"Error in opening file"<<endl;
   return 1;
}
// reading numbers
int num;
while(inFile>>num){
   arr[count] = num;
   count++;
}

inFile.close();
return count;
}

int divide(int arr[], int n, int num){
   int count = 0;
   for(int i=0; i<n; i++){
       if(arr[i]%num == 0)
           count++;
   }

   return count;
}

// Driver program to test above functions
int main()
{
// declaring an array of size 300
int arr[300];

int n = readData(arr);

int num;
cout<<"Enter a whole number in range 2-20 ? ";
cin>>num;

int count = divide(arr, n, num);

cout<<count<<" out of "<<n<<" in file were evenly divisible by "<<num<<endl;


return 0;
}

thirdweek thirdweek thirdweek g++ evenlyDivisible.cpp thirdweek./a.out Enter a whole number in range 2-20 ? 19 17 out of 271

Add a comment
Know the answer?
Add Answer to:
LAB8.DAT IS LISTED BELOW 154    -6   279   215   404   391  -166   199  -302  -469   244    &n
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;...

  • need help to complete this java program // add appropriate import statements here. // These imports...

    need help to complete this java program // add appropriate import statements here. // These imports you can leave as is. import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.canvas.Canvas; import javafx.scene.canvas.GraphicsContext; import javafx.scene.paint.Color; import javafx.stage.Stage; /** @author yourAccountNameHere */ public class ConnectTheDots extends Application {            /*     * Do not add code to main(). Add it below in connectTheDots instead.     */     public static void main(String[] args) {         launch(args);     }         /*...

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