Hi can anyone help?. I want to create a program that will call a series of functions to process a set of data. One function will read data into two arrays. A second will print the values stores in an array. Another will find the smallest of the values. A fourth will construct a new array from the two existing arrays. a fifth function will compare the values in the two arrays. I'm reading the elements from an input file and outputing the results to a separate file. The array can hold up to 50 elements. Please help. I'm new to C++ and I'm lost.This is my code:
this is my input data:
10 //this represents the number of elements in each array.
void readdata(int[], int[], int &);
void printarray(int[], int);
int smallest(int[], int);
void construct(int[], int[], int[], int);
void whatshigher(int[], int[], int, int &, int &, int &);
int main()
{
int n=10;
int numfirst, numsecond, numequal;
int first[50];
int second[50];
int third[50];
int newarr[n];
readdata(first, second, n);
fout<<"The first array:"<<endl;
printarray(first, n);
fout<<endl;
fout<<"The second array:"<<endl;
printarray(second, n);
fout<<endl;
fout<<"The smallest value in the first array is:";
fout<<smallest(first, n)<<endl;
fout<<"The smallest value in the second array is:";
fout<<smallest(second,n)<<endl;
fout<<endl;
construct(first,second,third, n);
fout<<"The smallest value in the third array is:";
fout<<smallest(third, n)<<endl;
fout<<endl;
whatshigher(first, second, n, numfirst, numsecond, numequal);
fout<<endl;
fout<<"The first array was larger "<< numfirst <<" time(s)"<<endl;
fout<<"The second array was larger "<< numsecond <<" time(s)"<<endl;
fout<<"The two arrays were equal "<< numequal <<" time(s)"<<endl;
//Function will compare the two arrays and determine which one has the higher value
void whatshigher(int one[], int two[], int n, int &numfirst, int &numsecond, int &numequal)
{
numfirst=0;
numsecond=0;
numequal=0;
for(int i=0; i<n; i++)
if(one[0]>two[0]){
fout<<"In position 0, the higher value in array one: "<< one[0]<<" is higher than "<<two[0]<<endl;
if(one[1]>two[1])
fout<<"In position 1, the higher value in array one: "<< one[1]<<" is higher than "<<two[1]<<endl;
if(one[2]>two[2])
fout<<"In position 2, the higher value in array one: "<< one[2]<<" is higher than "<<two[2]<<endl;
if(one[3]>two[3])
fout<<"In position 3, the higher value in array one: "<< one[3]<<" is higher than "<<two[3]<<endl;
if(one[4]>two[4])
fout<<"In position 4, the higher value in array one: "<< one[4]<<" is higher than "<<two[4]<<endl;
if(one[5]>two[5])
fout<<"In position 5, the higher value in array one: "<< one[5]<<" is higher than "<<two[5]<<endl;
if(one[6]>two[6])
fout<<"In position 6, the higher value in array one: "<< one[6]<<" is higher than "<<two[6]<<endl;
if(one[7]>two[7])
fout<<"In position 7, the higher value in array one: "<< one[7]<<" is higher than "<<two[7]<<endl;
if(one[8]>two[8])
fout<<"In position 8, the higher value in array one: "<< one[8]<<" is higher than "<<two[8]<<endl;
if(one[9]>two[9])
fout<<"In position 9, the higher value in array one: "<< one[9]<<" is higher than "<<two[9]<<endl;
numfirst++;
}
if(one[0]<two[0]){
fout<<"In position 0, the higher value in array two: "<< two[0]<<" is higher than "<<one[0]<<endl;
if(one[1]<two[1])
fout<<"In position 1, the higher value in array two: "<< two[1]<<" is higher than "<<one[1]<<endl;
if(one[2]<two[2])
fout<<"In position 2, the higher value in array two: "<< two[2]<<" is higher than "<<one[2]<<endl;
if(one[3]<two[3])
fout<<"In position 3, the higher value in array two: "<< two[3]<<" is higher than "<<one[3]<<endl;
if(one[4]<two[4])
fout<<"In position 4, the higher value in array two: "<< two[4]<<" is higher than "<<one[4]<<endl;
if(one[5]<two[5])
fout<<"In position 5, the higher value in array one: "<< two[5]<<" is higher than "<<one[5]<<endl;
if(one[6]<two[6])
fout<<"In position 6, the higher value in array one: "<< two[6]<<" is higher than "<<one[6]<<endl;
if(one[7]<two[7])
fout<<"In position 7, the higher value in array one: "<< two[7]<<" is higher than "<<one[7]<<endl;
if(one[8]<two[8])
fout<<"In position 8, the higher value in array two: "<< two[8]<<" is higher than "<<one[8]<<endl;
if(one[9]<two[9])
fout<<"In position 9, the higher value in array two: "<< two[9]<<" is higher than "<<one[9]<<endl;
numsecond++;
}
if(one[0]==two[0]){
fout<<"in position 0, the two arrays have the same value: "<< one[0]<<endl;
if(one[1]==two[1])
fout<<"in position 1, the two arrays have the same value: "<< one[1]<<endl;
if(one[2]==two[2])
fout<<"in position 2, the two arrays have the same value: "<< one[2]<<endl;
if(one[3]==two[3])
fout<<"in position 3, the two arrays have the same value: "<< one[3]<<endl;
if(one[4]==two[4])
fout<<"in position 4, the two arrays have the same value: "<< one[4]<<endl;
if(one[5]==two[5])
fout<<"in position 5, the two arrays have the same value: "<< one[5]<<endl;
if(one[6]==two[6])
fout<<"in position 6, the two arrays have the same value: "<< one[6]<<endl;
if(one[7]==two[7])
fout<<"in position 7, the two arrays have the same value: "<< one[7]<<endl;
if(one[8]==two[8])
fout<<"in position 8, the two arrays have the same value: "<< one[8]<<endl;
if(one[9]==two[9])
fout<<"in position 9, the two arrays have the same value: "<< one[9]<<endl;
numequal++;
}
return;
}
hi. I made that change and this is the result i'm getting.
There are 10 values in each array
The first array:
0 3670344 2009866572 3673896 3673928 3673888 0 0 16 16843042
The second array:
3670344 0 119 3670272 3670344 4 3670016 0 2293424 2009866324
The smallest value in the first array is:0
The smallest value in the second array is:0
the third array is:
0 0 -1344046508 -1999689728 -1617718208 14695552 0 0 36694784 697343784
The smallest value in the third array is:-1999689728
In position 0, the higher value in array two: 3670344 is higher than 0
In position 6, the higher value in array one: 3670016 is higher than 0
In position 8, the higher value in array two: 2293424 is higher than 16
In position 9, the higher value in array two: 2009866324 is higher than 16843042
The first array was larger 0 time(s)
The second array was larger 1 time(s)
The two arrays were equal 0 time(s)
Please use the # button when posting for code. Or put your code in code tags.
Now. I would suggest stripping your code down and working on your goal piece by piece. First, write the code to read your array from the file and print it to the console. Work through this until it's working before adding extra functionality. Alot of your code is highly verbose and not required.
if(one[0]==two[0]){
fout<<"in position 0, the two arrays have the same value: "<< one[0]<<endl;
if(one[1]==two[1])
fout<<"in position 1, the two arrays have the same value: "<< one[1]<<endl;
if(one[2]==two[2])
fout<<"in position 2, the two arrays have the same value: "<< one[2]<<endl;
if(one[3]==two[3])
fout<<"in position 3, the two arrays have the same value: "<< one[3]<<endl;
if(one[4]==two[4])
fout<<"in position 4, the two arrays have the same value: "<< one[4]<<endl;
if(one[5]==two[5])
fout<<"in position 5, the two arrays have the same value: "<< one[5]<<endl;
if(one[6]==two[6])
fout<<"in position 6, the two arrays have the same value: "<< one[6]<<endl;
if(one[7]==two[7])
fout<<"in position 7, the two arrays have the same value: "<< one[7]<<endl;
if(one[8]==two[8])
fout<<"in position 8, the two arrays have the same value: "<< one[8]<<endl;
if(one[9]==two[9])
fout<<"in position 9, the two arrays have the same value: "<< one[9]<<endl;
Can be written as:
1 2 3
for (int i = 0; i < 10; ++i)
if (one[i] == two[i])
fout << "in position " << i << ", the two arrays have the same value: " << one[i] << endl;