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:" <<...

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

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

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

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

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

  • // PLACE YOUR NAME HERE #include <iostream> using namespace std; float findAverage (int [], int); //...

    // PLACE YOUR NAME HERE #include <iostream> using namespace std; float findAverage (int [], int); // finds average of all //grades int findHighest (int [], int); // finds highest of all //grades int findFirstFail( int[]); int main() { int grades[100]; // the array holding 100 grades. int numberOfGrades; // the number of grades read. int pos; // index to the array. float avgOfGrades; // contains the average of the grades. int highestGrade; // contains the highest grade. int inderOfFail; //...

  • #include <iostream> using namespace std; bool binarySearch(int arr[], int start, int end, int target){ //your code...

    #include <iostream> using namespace std; bool binarySearch(int arr[], int start, int end, int target){ //your code here } void fill(int arr[], int count){ for(int i = 0; i < count; i++){ cout << "Enter number: "; cin >> arr[i]; } } void display(int arr[], int count){ for(int i = 0; i < count; i++){ cout << arr[i] << endl; } } int main() { cout << "How many items: "; int count; cin >> count; int * arr = new...

  • The following is a sample inventory in C++, i want to ask the user to input a item number for removing from inventory. //CPP #include <iostream> #include <fstream> #include <cstdlib>...

    The following is a sample inventory in C++, i want to ask the user to input a item number for removing from inventory. //CPP #include <iostream> #include <fstream> #include <cstdlib> #include <iomanip> #define MAX 1000 using namespace std; //Function to Add a new inventory item to the data into the array in memory void addItem(string desc[],string idNum[], float prices[], int qty[],int &num) { cout<<"Enter the names:"; cin>>desc[num]; cout<<"Enter the item number:"; cin>>idNum[num]; cout<<"Enter the price of item:"; cin>>prices[num]; cout<<"Enter 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
ADVERTISEMENT