This what I have so far. I need to read data from a file and i cannot use srand.
ID: 3858063 • Letter: T
Question
This what I have so far. I need to read data from a file and i cannot use srand. I am trying to get my functions to read data from a file i created called "lab9"
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>
#include <time.h>
#include <string>
using namespace std;
void highval(int num[]) // statement that ask user for input.
{
int hval = num[0]; // set max to first position in the array
int posMx = 0; // set counter to zero to start
for (int i =1; i<25; i++)
if (num[i] >hval)
{
hval = num[i];
posMx = i;
}
cout <<"The Highest Value is "<< hval <<" in position "<< posMx <<endl;
cout << endl;
}
void minVal(int num[]) // statement that ask user for input.
{
int lval = num[0]; // sets min to first position in array
int posMn= 0; //set counter to zero to start
for (int i =1; i<25; i++)
if (num[i] <lval)
{
lval = num[i];
posMn= i;
}
cout <<" The Lowest Value is "<< lval <<" in position "<< posMn <<endl;
cout << endl;
}
double avgV(int num[]) // statement that ask user for input.
{
int avg = 0;
double avgV;
for (int i= 0; i<25; ++i)
avg += num[i];
avgV = avg/25.0;
cout << "The Average is "<<avg/25 << endl;
cout << endl;
return avgV;
}
void print_Value(int num[])
{
int i;
cout << "Position" <<" "<<"Value"<<" "<<"Difference from the Average";
cout << endl;
for (int i=1; i<25;i++)
{
cout << i <<" "<<num[i]<<" "<<avgV <<endl;
}
cout << endl;
}
int main()
{
int num, i;
string filename;
ifstream lab9;
cout << "Enter file name ";
cin >> filename;
lab9.open (filename.c_str());
if (lab9. fail())
{
cerr << "error opening file" << filename << endl;
exit(1);
}
lab9 >> num;
int number_val[25];
cout <<endl;
highval(&num);
minVal(&num);
avgV(&num);
print_Value(&num);
lab9. close();
cout << endl;
cout << "This program coded by Lakeia Childress" << endl;
return 0;
}
INFO ON HOW TO DO LAB
Top of Form
Lab 9 – Processing an Array with Functions
Objectives:
· Define an array to store data (use random numbers this week)
· Design and code a function to generate random data in an array
· Design multiple functions to process the data in the array
· Design and code a function to print the data from the array
Instructions:
In this lab, you will be processing an array of 25 random numbers between 1-100 (do not use srand command so all results are the same). You will be required to write functions to:
Generate random numbers in the array
Find and print the highest value in the array and its position in the array
Find and print the lowest number in the array and its position in the array
Find the average of the values in the array – return the value to main
Print the array position, the value in the array, and its difference from the average for all 25 positions in the array. Put a heading on the top of this table of information.
Table Format:
Position Value Difference from the Average
0 ? +/- ???
1 ? +/- ???
Data:
Random numbers
Run:
Run the program and be sure to check the results to see if they are correct.
Position Value Difference from the Average
0 ? +/- ???
1 ? +/- ???
Explanation / Answer
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>
#include <time.h>
#include <string>
using namespace std;
void highval(int num[]) // statement that ask user for input.
{
int hval = num[0]; // set max to first position in the array
int posMx = 0; // set counter to zero to start
for (int i =1; i<25; i++)
if (num[i] >hval)
{
hval = num[i];
posMx = i;
}
cout <<"The Highest Value is "<< hval <<" in position "<< posMx <<endl;
cout << endl;
}
void minVal(int num[]) // statement that ask user for input.
{
int lval = num[0]; // sets min to first position in array
int posMn= 0; //set counter to zero to start
for (int i =1; i<25; i++)
if (num[i] <lval)
{
lval = num[i];
posMn= i;
}
cout <<" The Lowest Value is "<< lval <<" in position "<< posMn <<endl;
cout << endl;
}
double avgV(int num[]) // statement that ask user for input.
{
int avg = 0;
double avgV;
for (int i= 0; i<25; ++i)
avg += num[i];
avgV = avg/25.0;
cout << "The Average is "<<avg/25 << endl;
cout << endl;
return avgV;
}
void print_Value(int num[])
{
int i;
cout << "Position" <<" "<<"Value"<<" "<<"Difference from the Average";
cout << endl;
for (int i=1; i<25;i++)
{
cout << i <<" "<<num[i]<<" "<<avgV <<endl;
}
cout << endl;
}
int main()
{
int num, i=0;;
string filename;
ifstream lab9;
int number_val[25];
cout << "Enter file name ";
cin >> filename;
lab9.open (filename.c_str());
if (lab9. fail())
{
cerr << "error opening file" << filename << endl;
exit(1);
}
else
{
while(i<25)
{
lab9>>number_val[i];
i++;
}
}
cout <<endl;
highval(number_val);
minVal(number_val);
avgV(number_val);
print_Value(number_val);
lab9. close();
cout << endl;
cout << "This program coded by Lakeia Childress" << endl;
return 0;
}
=========================================
input.txt
1
2
3
4
5
1
2
3
4
5
1
2
3
4
5
1
2
3
4
5
1
2
3
4
5
======================================
Output:
akshay@akshay-Inspiron-3537:~/Chegg$ g++ fileh.cpp
akshay@akshay-Inspiron-3537:~/Chegg$ ./a.out
Enter file name input.txt
The Highest Value is 5 in position 4
The Lowest Value is 1 in position 0
The Average is 3
Position Value Difference from the Average
1 2 1
2 3 1
3 4 1
4 5 1
5 1 1
6 2 1
7 3 1
8 4 1
9 5 1
10 1 1
11 2 1
12 3 1
13 4 1
14 5 1
15 1 1
16 2 1
17 3 1
18 4 1
19 5 1
20 1 1
21 2 1
22 3 1
23 4 1
24 5 1
This program coded by Lakeia Childress
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.