d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Really Fast Help Needed Plz > Topic
Add Reply New Topic New Poll
Member
Posts: 2,881
Joined: Nov 3 2009
Gold: 1,378.00
Dec 9 2012 01:08pm
hey guys, i have a question, heres my program and what i need to do.

http://www.sci.brooklyn.cuny.edu/~jones/cisc1110/a5f12.pdf <----what i need to do

i have errors while trying to run this program on stuff not being declared, but i dont know how and where to declare it, can some1 help me please? its a really fast 5 mins top question. its urgent, i have a test on this program and i need to fix this program asap, ty!!^

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

void readdata(int &, int[], int[]);
void printarr(int, int[]);
int smallest(int, int[]);
void construct(int, int[], int[], int &);
void whatshigher(int[], int[], int, int &, int &, int &);

ifstream infile5;
ofstream logFile;

int main()
{
int n = 0;

infile5.open("Scores.txt");
logFile.open("Log.txt");

int counter1 = 0;
int counter2 = 0;
int counter3 = 0;

readdata(n, score1, score2);

logFile<<"There are " <<n<<" values in each array"<<endl;

logFile<<"Score1 array: ";
printarr(n, score1);

logFile<<"Score2 array: ";
printarr(n, score2);
logFile<<endl;

logFile<<"The smallest value in scores1 is: "<<smallest(n, score1)<<endl;
logFile<<"The smallest value in scores2 is: "<<smallest(n, score2)<<endl<<endl;

logFile<<"The scoresum array: ";
construct(n, score1, score2, scoresum);

logFile<<"The smallest value in scoresum is: "<<smallest(n, scoresum)<<endl<<endl;

whatshigher(score1, score2, n, counter1, counter2, counter3);

logFile<<"Scores1 was larger "<<counter1<<" times"<<endl;
logFile<<"Scores2 was larger "<<counter2<<" times"<<endl;
logFile<<"The two scores were equal "<<counter3<<" times"<<endl;

logFile.close();

return 0;
}

void readdata(int &n, int arr1[], int arr2[])
{
infile5 >> n;

for(int i=0;i < n;i++)
{
infile5 >> arr1[i];
infile5 >> arr2[i];
}
return;
}

void printarr(int num, int vals[])
{
for(int i = 0; i < num; ++i)
{
logFile<< vals[i] << " ";
}
logFile<<endl;
return;
}

int smallest(int lim, int nums[])
{
int lowest = nums[0];
for(int i = 1; i < lim; ++i)
{
if(nums[i] < lowest)
{
lowest = nums[i];
}
}
return lowest;
}

void construct(int k, int first[], int second[], int *&sumarr)
{
sumarr = new int[k-1];
for(int i = 0; i < k; ++i)
{
sumarr[i] = first[i] + second[i];
}

printarr(k, sumarr);
return;
}

void whatshigher(int first[], int second[], int n, int &counter1, int &counter2, int &counter3)
{
for(int i = 0; i < n; ++i)
{
if(first[i] > second[i])
{
logFile<<"In position "<<i<<", array score1 is larger: "<< first[i] << " is greater than " << second[i]<<endl;
counter1++;
}
else if(first[i] < second[i])
{
logFile<<"In position "<<i<<", array score2 is larger: "<< second[i] << " is greater than " << first[i]<<endl;
counter2++;
}
else
{
logFile<<"In position "<<i<<", the two arrays have the same value: " << first[i]<<endl;
counter3++;
}
}
logFile<<endl;
return;
}

Member
Posts: 2,425
Joined: Aug 17 2007
Gold: 3,754.50
Dec 9 2012 01:54pm
Without getting to far into it, you need to declare variables for score1, score2, and scoresum.

In earlier CS classes this would be done like this:

Code
int score1[999], score2[999], scoresum[999];

Later on you would be expected to create the arrays dynamically so you would have this:

Code
int *score1, *score2, *scoresum;

In the first case, you would just pass the arrays to your read functions like you do.

In the second case you would have to use the format you have in your "construct" function: int *&sumarr
Also your read functions will have to create the arrays. So after you read in "n", you would have to have:

Code
arr1 = new int[n];
arr2 = new int[n];


LMK if this helps or if you have any more questions.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll