Using C and codeblocks This assignment is to demonstrate the
Using C++ and codeblocks:
This assignment is to demonstrate the use of FUNCTIONS and ARRAYS.
Write a program that meets the following requirements:
1. Read in a maximum of 50 records, each record will be on a separate line using columns one and two.
Example:
36
O1
5H
2. Determine if each record is a positive integer. Keep track of all records that are not positive integers and print a list of them.
For all the positive integer data records:
3. Determine the average (mean) rounded to the nearest whole number, median, mode and standard deviation.
4. Compute the difference between each of the positive integers and the mean and store the difference in an array corresponding to the original integer number.
5. Determine which of the integers deviate’s the most from the mean, print the number and how much it deviates.
6. Print the original list of integers along side the list of integers sorted from the lowest to the highest.
7. Print a bar graph indicating how much each integer deviates from the mean. Use the sorted list of integers to develop the graph.
8. Label and print all of the above findings.
Your program must have separate functions to determine each of the following: mean, median, mode, standard deviation, reading of the original data, printing of the invalid records, printing of the valid records, number 6 and 7 above.
Your program will have its final test with the integers from the data file data6.txt.
Demonstrate your output in lab.
Submit your source code and algorithm or flowchart in blackboard.
All values must be clearly labeled in the output.
You are required to demonstrate your output in Lab.
Documentation will be 20% of your grade.
Your source code must contain the following documentation.
1. Header information: (Source Code and Output)
Your Name, course & section number, assignment number and due date.
2. A brief description of your assignment.
3. Variable dictionary: A brief description of every variable used in your program.
4. Comments related to input and output of data and all formulas used in your code.
Solution
#include <iostream>
#include <cmath>
#include <fstream>
// Assignment shell
using namespace std;
int good[40] = { 0 }, ctg = 0,ctb =0;
char bad[20][3] = { \' \'};
int *t;
//Read data from file
void readdata(ifstream &f1,int good[],char bad[][3], int &ctg, int&ctb)
{
char ch1,ch2;
cout<<\"\ Original Data: \ \";
while(f1>>ch1>>ch2, !f1.eof())
{
cout << ch1 << ch2<< \" \";
//If both are integer number store it in good array
if ( ch1>= \'0\' && ch1 <= \'9\' && ch2 >=\'0\' && ch2 <=\'9\')
{
good[ctg] = (int(ch1) - 48) * 10 + ( int (ch2) - 48);
ctg++;
}
//If any one not integer number store it in bad array
else
{
bad[ctb][0] = ch1;
bad[ctb][1] = ch2;
bad[ctb][2] = 0;
ctb++;
}
}
}
//Returns the mean value
int GetMean()
{
double dS = good[0];
for (int i = 1; i < ctg; ++i)
{
dS += good[i];
}
return dS/ctg;
}
//Returns the Median value
int GetMedian()
{
// Allocate an array of the same size and sort it.
t = new int[ctg];
for (int i = 0; i < ctg; ++i)
{
t[i] = good[i];
}
for (int i = ctg - 1; i > 0; --i)
{
for (int j = 0; j < i; ++j)
{
if (t[j] > t[j+1])
{
int dT = t[j];
t[j] = t[j+1];
t[j+1] = dT;
}
}
}
// Middle or average of middle values in the sorted array.
int dM = 0.0;
if ((ctg % 2) == 0)
{
dM = (t[ctg/2] + t[(ctg/2) - 1])/2.0;
}
else
{
dM = t[ctg/2];
}
delete [] t;
return dM;
}
//Returns the Mode value
int GetMode()
{
// Allocate an int array of the same size to hold the repetition count
int* t = new int[ctg];
for (int i = 0; i < ctg; ++i)
{
t[i] = 0;
int j = 0;
bool Found = false;
while ((j < i) && (good[i] != good[j]))
{
if (good[i] != good[j])
{
++j;
}
}
++(t[j]);
}
int iM = 0;
for (int i = 1; i < ctg; ++i)
{
if (t[i] > t[iM])
{
iM = i;
}
}
delete [] t;
return good[iM];
}
//Returns the Variance
int CalculateVariane()
{
int mean = GetMean();
double t = 0;
for(int i = 0; i < ctg; i++)
{
t += (good[i] - mean) * (good[i] - mean) ;
}
return t / ctg;
}
//Returns the Standard Deviation
int GetStandardDeviation()
{
return sqrt(CalculateVariane());
}
//Calculates th difference from mean and display
void diff(int m)
{
int* t = new int[ctg];
int c;
for(c = 0; c < ctg; c++)
t[c] = good[c] - m;
cout<<\"\ After finding the difference from Mean: \ \";
for(c = 0; c < ctg; c++)
cout<<\" \"<<t[c];
}
//Print lowest to highest
void printLH()
{
int Temp, x, y;
for(x = 0; x < ctg; x++)
{
for(y = 0; y < ctg - x - 1; y++)
{
if(good[y] > good[y + 1])
{
Temp = good[y];
good[y] = good[y + 1];
good[y + 1] = Temp;
}
}
}
for(x = 0; x < ctg; x++)
{
cout<<\" \"<<good[x];
}
}
//Prints Graph
void printGraph()
{
cout<<\"\ For negative @ \\t For positive * \ \";
int c, d;
for(c = 0; c < ctg; c++)
{
if(t[c] > 0)
{
for(d = 0; d < t[c]; d++)
cout<<\" * \";
cout<<endl;
}
else
{
for(d = t[c]; d < 0; d++)
cout<<\" @ \";
cout<<endl;
}
}
}
int main()
{
ifstream f1 (\"Data6.txt\",ios::in);
readdata(f1,good,bad,ctg,ctb);
cout<< \"\ \ Good Data \ \";
for(int i = 0; i <ctg; i++)
cout << good[i]<< \" \";
cout << \"\ \ Bad Data \ \";
for (int j = 0 ; j < ctb; j++)
cout <<bad[j]<<\" \";
f1.close();
cout<<\"\ Mean: \"<<GetMean();
cout<<\"\ Median: \"<<GetMedian();
cout<<\"\ Mode: \"<<GetMode();
cout<<\"\ Standard Deviation: \"<<GetStandardDeviation();
diff(GetMean());
cout<<\"\ Print Lowest to Highest: \ \";
printLH();
printGraph();
return 0;
}
Output:
Original Data:
31 02 99 1l 05 O5 76 33 34 B3 87 56 46 76 Il 75 65 93 65 01 02 05 98 11 12 73 15 19 ll lY 97 9l 76 81 87 45 43 76 69 23 35 31 37 ll pq 03 76 02
Good Data
31 2 99 5 76 33 34 87 56 46 76 75 65 93 65 1 2 5 98 11 12 73 15 19 97 76 81 87 45 43 76 69 23 35 31 37 3 76 2
Bad Data
1l O5 B3 Il ll lY 9l ll pq
Mean: 47
Median: 45
Mode: 76
Standard Deviation: 32
After finding the difference from Mean:
-16 -45 52 -42 29 -14 -13 40 9 -1 29 28 18 46 18 -46 -45 -42 51 -36 -35 26 -32 -28 50 29 34 40 -2 -4 29 22 -24 -12 -16 -10 -44 29 -45
Print Lowest to Highest:
1 2 2 2 3 5 5 11 12 15 19 23 31 31 33 34 35 37 43 45 46 56 65 65 69 73 75 76 76 76 76 76 81 87 87 93 97 98 99
For negative @ For positive *
@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
* * * * * * * * * * * * * * * * * * * * * * * * * * * * *
@ @ @ @ @ @ @ @ @ @ @ @ @ @
@ @ @ @ @ @ @ @ @ @ @ @ @
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * *
@
* * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * *
@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
* * * * * * * * * * * * * * * * * * * * * * * * * *
@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
@ @
@ @ @ @
* * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * *
@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
@ @ @ @ @ @ @ @ @ @ @ @
@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
@ @ @ @ @ @ @ @ @ @
@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
* * * * * * * * * * * * * * * * * * * * * * * * * * * * *
@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @





