1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
|
//assignment #5
#include<iostream>
#include<fstream>
using namespace std;
ifstream infile("data.dat");
const int VALUE=50;
void readdata(int, int[], int[]);
void printarray(int, int[]);
int smallest(int, int[]);
void construct(int , int [], int [], int []);
int whatshigher(int, int [], int [], int , int , int );
int main()
{
int n, small, small_1, small_diff, score1[VALUE], score2[VALUE];
int difference[VALUE], score1_count=0, score2_count=0, equal_count=0;
infile>>n;
readdata(n, score1, score2);
cout<<"Score 1 array is printing"<<endl;
printarray(n, score1);
cout<<"Score 2 array is printing"<<endl;
printarray(n, score2);
small=smallest(n, score1);
cout<<"smallest in array 1"<<endl;
cout<<small<<endl;
small_1=smallest(n, score2);
cout<<"smallest in array 2"<<endl;
cout<<small_1<<endl;
construct(n, score1, score2, difference);
cout<<"The smallest in difference: ";
small_diff=smallest(n, difference);
cout<<small_diff<<endl;
whatshigher(n, score1, score2, score1_count, score2_count, equal_count);
cout<<"Array score1 was larger "<<score1_count<<"times"<<endl;
cout<<"Array score2 was larger "<<score2_count<<"times"<<endl;
cout<<"The two arrays were equal"<<equal_count<<"times"<<endl;
infile.close();
system("pause");
return 0;
}
//readdata()
void readdata(int n, int nums1[], int nums2[]){
for(int i=0; i<n; i++){
infile>>nums1[i];
infile>>nums2[i];
}
}
//printarray()
void printarray(int lim, int arr[]){
for(int i=0; i<lim; i++)
cout<<arr[i]<<" ";
cout<<endl;
}
//smallest()
int smallest(int n, int nums[]){
//Process array
int min ;
for(int i=0; i<n; i++)
{ int num = nums[i];
if(num < min)
min = num;
}
return min;
}
//construct()
void construct(int k, int old1[], int old2[], int diff[]){
cout<<"The difference array: ";
for(int i=0; i<k; i++){
diff[i]=old1[i]-old2[i];
cout<<diff[i]<<" ";
}
cout<<endl;
}
//whatshigher()
int whatshigher(int n, int score1[], int score2[], int c1, int c2, int c3){
for(int i=0; i<n; i++){
if(score1[i]>score2[i]){
cout<<"In position "<<i<<", score1 is larger:"
<<score1[i]<<" is greater than "<<score2[i]<<endl;
c1++;
}
else if(score2[i]>score1[i]){
c2++;
cout<<"In position "<<i<<", score2 is larger:"
<<score2[i]<<" is greater than "<<score1[i]<<endl;
}
else if(score1[i]==score2[i]){
cout<<"In position "<<i<<", the two arrays have the same value: "
<<score1[i]<<endl;
c3++;
}
}
return c1, c2, c3;
}
|