Programs must be written in C++: Program 1: (Practicing an example of function u
ID: 3573822 • Letter: P
Question
Programs must be written in C++:
Program 1:
(Practicing an example of function using call by reference) Write a program that reads a set of information related to students in C++ class and prints them in a table format. The information is stored in a file called data.txt. Each row of the file contains student number, grade for assignment 1, grade for assignment 2, and grade for assignment 3. Your main program should read each row, pass the grades for the three assignments to a function called ProcessARow to calculate the average of the grades, minimum of the three assignments, and the maximum of the three assignments. The results (average, maximum, and minimum) should be returned to the main program and the main program prints them on the screen in a table format. For example, if the file includes 126534 9 8 10 321345 7 3 5 324341 9 9 9 your program should print Std-Id A1 A2 A3 Min Max Average ------------------------------------------------------------------------- 126534 9 8 10 8 10 9.0 321345 7 3 5 3 7 5.0 324341 9 9 9 9 9 9.0 You must use call by reference to do the above question. _________________________________________________________________
Program 2: (Practicing an example of function using call by value) Repeat the above question using call by value. This means you need to have three different functions: one to calculate the average, another to calculate the minimum, and the third one to calculate the maximum. This is how to call these functions: Max = CalculateMax(A1,A2,A3); Min = CalculateMin(A1,A2,A3); Average = CalculateAvg(A1,A2,A3); _________________________________________________________________
Program 3: Write a program with several functions that performs the following tasks. : a. Create a function that reads the following 10 integer numbers from the file data.txt into array A. 10 15 27 89 90 95 27 13 99 33 b. Copy array A into array B in reverse order c. Print array A. d. Print array B. e. Find the number of elements in array A that are >= 80 and <=100. f. Find the number of the elements in array A in which their contents are divisible by 5 g. Find the index of the elements in array A in which their contents are divisible by 5. h. Find mean (average) in array A. i. Find the minimum number in array A. j. Ask the user to input a key. Then search for the key in array A and inform the user about the existence (true / false) of the key in array Your program should include several functions. • A function for filling up the information into an array (part a calls this function) • A function that does the copying of one array into another in reverse order. Arrays must have the same size. (part b calls this function) • Printing any array with any size ( part c and d should call this function) • finding and returning the number of elements between 80 and 100 in any array with any size (part e calls this function) • finding and returning the number of elements in an array that are divisible by 5 (part f calls this function) • printing the index of elements in an array that are divisible by 5 (part g calls this function) • finding and returning the mean (average) of elements in any array with any size (part h calls this function) • finding and returning the minimum number in any array of any size (part i calls this function) • searching for a key in any array of any size and returning true/false as result (part j calls this function) Note: If your function is supposed to return a value, then do not print anything inside the function. It would be the job of the main program to print the result on the screen. However, if the function is supposed to print information (like the print function), then you can use cout statement inside the function to print the related information. Also, any parameter that is not supposed to be changed inside a function must be declared as constant parameter.
Explanation / Answer
Program 1:
#include<iostream>
#include<fstream>
#include<stdlib.h>
using namespace std;
//Calculate maximum call by reference
void CalculateMax(int A1, int A2, int A3, int &ma)
{
//Find out maximum
if(A1 > A2 && A1 > A3)
ma = A1;
else if(A2 > A3)
ma = A2;
else
ma = A3;
}
//Calculate minimum call by reference
void CalculateMin(int A1, int A2, int A3, int &mi)
{
//Find out minimum
if(A1 < A2 && A1 < A3)
mi = A1;
else if(A2 < A3)
mi = A2;
else
mi = A3;
}
//Calculate average call by reference
void CalculateAvg(int A1, int A2, int A3, float &avg)
{
//Calculate average
avg = ((A1 + A2 + A3) / 3);
}
//Displays student information in tabular format with minimum, maximum and average
void show(int r[], int as1[], int as2[], int as3[], int no)
{
int mi, ma, x;
float avg;
cout<<" Std-Id A1 A2 A3 Min Max Average ";
cout<<" ------------------------------------------------------------------------- ";
for(int x = 0; x < no ; x++)
{
CalculateMin(as1[x], as2[x], as3[x], mi);
CalculateMax(as1[x], as2[x], as3[x], ma);
CalculateAvg(as1[x], as2[x], as3[x], avg);
cout<<r[x]<<" "<<as1[x]<<" "<<as2[x]<<" "<<as3[x]<<" "<<mi<<" "<<ma<<" "<<avg<<endl;
}
}
//Reads a file and returns number of record
int readFile(int r[], int as1[], int as2[], int as3[])
{
ifstream Rfile;
//Opens data.txt file for reading
Rfile.open("data.txt");
int n = 0;
//Check file can be open or not
if (Rfile.is_open())
{
//Reads data till end of file
while (!Rfile.eof()) //Checks end of file
{
//Reads and stores data in array
Rfile >>r[n];
Rfile>>as1[n];
Rfile>>as2[n];
Rfile>>as3[n];
//Increases the record count
n++;
}
}
//File not found error
else
{
cout<<"ERROR: Data file not found.";
exit(0);
}
//Close file
Rfile.close();
return n;
}
int main()
{
int regno[10], as1[10], as2[10], as3[10], n;
n = readFile(regno, as1, as2, as3);
show(regno, as1, as2, as3, n);
}
Output:
Std-Id A1 A2 A3 Min Max Average
-------------------------------------------------------------------------
126534 9 8 10 8 10 9
321345 7 3 5 3 7 5
324341 9 9 9 9 9 9
Program 2:
#include<iostream>
#include<fstream>
#include<stdlib.h>
using namespace std;
//Calculate maximum call by value
int CalculateMax(int A1, int A2, int A3)
{
int ma;
//Find out maximum
if(A1 > A2 && A1 > A3)
ma = A1;
else if(A2 > A3)
ma = A2;
else
ma = A3;
return ma;
}
//Calculate minimum call by value
int CalculateMin(int A1, int A2, int A3)
{
int mi;
//Find out minimum
if(A1 < A2 && A1 < A3)
mi = A1;
else if(A2 < A3)
mi = A2;
else
mi = A3;
return mi;
}
//Calculate average call by value
float CalculateAvg(int A1, int A2, int A3)
{
//Calculate average
return ((A1 + A2 + A3) / 3);
}
//Displays student information in tabular format with minimum, maximum and average
void show(int r[], int as1[], int as2[], int as3[], int no)
{
int mi, ma, x;
float avg;
cout<<" Std-Id A1 A2 A3 Min Max Average ";
cout<<" ------------------------------------------------------------------------- ";
for(int x = 0; x < no ; x++)
{
mi = CalculateMin(as1[x], as2[x], as3[x]);
ma = CalculateMax(as1[x], as2[x], as3[x]);
avg = CalculateAvg(as1[x], as2[x], as3[x]);
cout<<r[x]<<" "<<as1[x]<<" "<<as2[x]<<" "<<as3[x]<<" "<<mi<<" "<<ma<<" "<<avg<<endl;
}
}
//Reads a file and returns number of record
int readFile(int r[], int as1[], int as2[], int as3[])
{
ifstream Rfile;
//Opens data.txt file for reading
Rfile.open("data.txt");
int n = 0;
//Check file can be open or not
if (Rfile.is_open())
{
//Reads data till end of file
while (!Rfile.eof()) //Checks end of file
{
//Reads and stores data in array
Rfile >>r[n];
Rfile>>as1[n];
Rfile>>as2[n];
Rfile>>as3[n];
//Increases the record count
n++;
}
}
//File not found error
else
{
cout<<"ERROR: Data file not found.";
exit(0);
}
//Close file
Rfile.close();
return n;
}
int main()
{
int regno[10], as1[10], as2[10], as3[10], n;
n = readFile(regno, as1, as2, as3);
show(regno, as1, as2, as3, n);
}
Output:
Std-Id A1 A2 A3 Min Max Average
-------------------------------------------------------------------------
126534 9 8 10 8 10 9
321345 7 3 5 3 7 5
324341 9 9 9 9 9 9
Program 3:
#include<iostream>
#include<fstream>
#include<stdlib.h>
using namespace std;
//Reads a file and returns number of record
int readFile(int A[])
{
ifstream Rfile;
//Opens data1.txt file for reading
Rfile.open("data1.txt");
int n = 0;
//Check file can be open or not
if (Rfile.is_open())
{
//Reads data till end of file
while (!Rfile.eof()) //Checks end of file
{
//Reads and stores data in array
Rfile >>A[n];
//Increases the record count
n++;
}
}
//File not found error
else
{
cout<<"ERROR: Data file not found.";
exit(0);
}
//Close file
Rfile.close();
return n;
}
//Copy the array contents to another array
void copyArrayReverse(int fi[], int se[], int n)
{
//Starts from last index position of first array and stores it in the first position of the second array
//Decrease the counter for the first array and increase the counter for the second array
for(int x = n - 1, y = 0; x >= 0; x--, y++)
{
se[y] = fi[x];
}
}
//Displays the content of array
void disp(int a[], int n)
{
for(int c = 0; c < n; c++)
cout<<a[c]<<" ";
}
//Displays the values which are divisible by 5
void divisible5(int f[], int n)
{
for(int x = 0; x < n; x++)
//Checks for the divisible by 5
if(f[x] % 5 == 0)
cout<<" "<<f[x];
}
//Displays the index of the array which are divisible by 5
void divisibleIndex5(int f[], int n)
{
for(int x = 0; x < n; x++)
//Checks for the divisible by 5
if(f[x] % 5 == 0)
cout<<" "<<x;
}
//Returns the mean average
int meanAverage(int f[], int n)
{
int tot = 0;
for(int x = 0; x < n; x++)
tot += f[x];
return (tot/n);
}
//Returns true if the search number is found otherwise false
bool Available(int f[], int n, int searchNo)
{
int fo = 0;
for(int x = 0; x < n; x++)
//Check for the availability of the number in the array
if(f[x] == searchNo)
{
fo = 1;
break;
}
if(fo == 1)
return true;
else
return false;
}
//Displays the numbers between 80 and 100 including both
void Between(int f[], int n)
{
for(int x = 0; x < n; x++)
//Check for the range
if(f[x] >= 80 && f[x] <= 100)
cout<<" "<<f[x];
}
int main()
{
int First[20], Second[20], noData, no;
noData = readFile(First);
copyArrayReverse(First, Second, noData);
cout<<" Display First Array: ";
disp(First, noData);
cout<<" Display Second Array: ";
disp(Second, noData);
cout<<" Number of elements >= 80 and <= 100: ";
Between(First, noData);
cout<<" Numbers of Array Divisible by 5 ";
divisible5(First, noData);
cout<<" Index of Array Divisible by 5 ";
divisibleIndex5(First, noData);
cout<<" Mean Average of Array: "<<meanAverage(First, noData);
cout<<" Maximum Numbers in Array: "<<noData;
cout<<" Enter a number to search: ";
cin>>no;
if(Available(First, noData, no))
cout<<" Number is Available: "<<no;
else
cout<<" Number is Not Available: "<<no;
}
Output:
Display First Array: 10 15 27 89 90 95 27 13 99 33
Display Second Array: 33 99 13 27 95 90 89 27 15 10
Number of elements >= 80 and <= 100: 89 90 95 99
Numbers of Array Divisible by 5
10 15 90 95
Index of Array Divisible by 5
0 1 4 5
Mean Average of Array: 49
Maximum Numbers in Array: 10
Enter a number to search: 89
Number is Available: 89
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.