Question

Thank you!

Goals: Practicing arrays and functions 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 Create a program that will read values from a file called Lab8.dat (posted on Canvas) and store it 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 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). ount how many values in the s function should use a loop to c 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 how many values in the file are eveniy 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 12 with the underlined values replaced by the your calculated values and the users input.

/*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;

for(int j=0; j < sum; j++)
{
  int div_by, count;
  if(arr[j]/num == 0)
  {
   count += j;
   cout << count;
  }  

  cout << "Out of " << sum << " values " << count
  << " are divisible by " << num << " ." << endl;
}

return sum;
}

This is what I am working with at the moment. I am not sure how the function is to have 3 parameters but then it says to call the function seperately for reading the array and calculating the numbers divisible by the whole number entered by the user.

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

/*Lab 8 : Practicing functions and arrays
Purpose:
*/

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

int read_function(int arr[]);
int countDivisible (int arr[], int, int);

int main()
{
int array[300], numba, count;

count = read_function(array);

cout << "Enter a whole number between 2-20: " << endl;
cin >> numba;

int divisible = countDivisible(array, count, numba);

cout << divisible << " of the " << count << " values in the file were evenly divisible by " << numba << "\n";

return 0;
}

int read_function (int arr[])
{
ifstream array_file("Lab8.dat");

int num = 0;
while(array_file >> arr[num])
{
num++;
}

return num;
}

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

Add a comment
Know the answer?
Add Answer to:
Thank you! /*Lab 8 : Practicing functions and arrays Purpose: */ #include<iostream> #include<fstream> using namespace std;...
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
  • #include <iostream> #include <fstream> using namespace std; //constants const int CAP = 100; //function prototypes bool...

    #include <iostream> #include <fstream> using namespace std; //constants const int CAP = 100; //function prototypes bool openFile(ifstream &); void readData(ifstream &, int [], int &); void printData(const int [], int); void sum(const int[], int); void removeItem(int[], int &, int); int main() { ifstream inFile; int list[CAP], size = 0; if (!openFile(inFile)) { cout << "Program terminating!! File not found!" << endl; return -1; } //read the data from the file readData(inFile, list, size); inFile.close(); cout << "Data in file:" <<...

  • How would answer question #4 of this ? #1 and 2 #include<iostream> #include <fstream> using namespace...

    How would answer question #4 of this ? #1 and 2 #include<iostream> #include <fstream> using namespace std; void read(int arr[]) {    string line;    ifstream myfile("input.txt");    int i = 0;    if (myfile.is_open())    {        while (getline(myfile, line))        {            arr[i];            i++;        }        myfile.close();    } } void output(int arr[]) {    ofstream myfile("output.txt");    if (myfile.is_open())    {        int i = 0;...

  • can you please split this program into .h and .cpp file #include <iostream> #include<string> #include<fstream> #define...

    can you please split this program into .h and .cpp file #include <iostream> #include<string> #include<fstream> #define SIZE 100 using namespace std; //declare struct struct word_block {    std::string word;    int count; }; int getIndex(word_block arr[], int n, string s); int main(int argc, char **argv) {    string filename="input.txt";    //declare array of struct word_block    word_block arr[SIZE];    int count = 0;    if (argc < 2)    {        cout << "Usage: " << argv[0] << "...

  • use c++ and complete this lab #include <iostream> using namespace std; class FibObj { public: void...

    use c++ and complete this lab #include <iostream> using namespace std; class FibObj { public: void setSize() { cout << "Enter an integer larger than 2: "; cin >> size; } int getSize() { return size; } void fibTerms(int x) // Write a recursive function { } explicit FibObj() // Default constructor was made explicit to avoid possible implicit type conversion { setSize(); int *arr; arr = new int[getSize()]; arr[0] = 1; arr[1] = 1; // Call recursive function here...

  • #include <fstream> #include <iostream> #include <cstdlib> using namespace std; // Place charcnt prototype (declaration) here int...

    #include <fstream> #include <iostream> #include <cstdlib> using namespace std; // Place charcnt prototype (declaration) here int charcnt(string filename, char ch); int main() { string filename; char ch; int chant = 0; cout << "Enter the name of the input file: "; cin >> filename; cout << endl; cout << "Enter a character: "; cin.ignore(); // ignores newline left in stream after previous input statement cin.get(ch); cout << endl; chcnt = charcnt(filename, ch); cout << "# of " «< ch« "'S:...

  • #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str);...

    #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str); int numConsonants(char *str); int main() {    char string[100];    char inputChoice, choice[2];    int vowelTotal, consonantTotal;    //Input a string    cout << "Enter a string: " << endl;    cin.getline(string, 100);       do    {        //Displays the Menu        cout << "   (A) Count the number of vowels in the string"<<endl;        cout << "   (B) Count...

  • Question 1: Fix the 2D dynamic array initialization in following code #include <iostream> using namespace std;...

    Question 1: Fix the 2D dynamic array initialization in following code #include <iostream> using namespace std; int main(){    int rows = 5; int cols = 5; int x;    int** arr = new int[rows][cols]    cin >> x; arr[x][x] = x; cout << "arr[x][x] = " << arr[x][x];    return 0; } Question 2: Fix the code to initialize the 2D array elements to x #include <iostream> using namespace std; int main(){    int rows; int cols; int x;...

  • c++ #include using namespace std; int main() {    int n, x, num, j = 0;...

    c++ #include using namespace std; int main() {    int n, x, num, j = 0;    cin >> n >> x;    int*arr = new int[n];    for (int i = 0; i < n; i++)    {        cin >> num;        if (num < x)        {            arr[j] = num;            j++;        }    }    for (int i = 0; i < j; i++)    {...

  • This is the creatList and printList function #include <iostream> #include <fstream> using namespace std; struct nodeType...

    This is the creatList and printList function #include <iostream> #include <fstream> using namespace std; struct nodeType {    int info;    nodeType *link;    nodeType *next;    double value; }; void createList(nodeType*& first, nodeType*& last, ifstream& inf); void printList(nodeType* first); int main() {    nodeType *first, *last;    int num;    ifstream infile;    infile.open("InputIntegers.txt");    createList(first, last, infile);    printList(first);    infile.close();    system("pause");    return 0; } void createList(nodeType*& first, nodeType*& last, ifstream& infile) {    int number;...

  • 4) What is the output if the input istom - Sawyer? #include <iostream> using namespace std;...

    4) What is the output if the input istom - Sawyer? #include <iostream> using namespace std; int main() { string playerName; cout << "Enter name"; cin >> playerName; cout << endl « playerName; return 0; } a. Tom - Sawyer b. Tom Sawyer c. Tom d. Sawyer 5) Which XXX generates "Adam is 30 years old." as the output? #include <iostream> using namespace std; int main() { string name = "Adam"; int age = 30; XXX return 0; } a....

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