int da_getElement (int * a, int n, int i){
int j=0;
if (i<=n){
while (j<(n-1)){
a=a+j;
if (a=&a[i]){
return a[i];
}
j++;
}
}
else {
cerr<<"the index you entered is larger than the size of the array"<<endl;
}
return 0;
}
i know that the problem is in lines 5 and 6 but i dont know how to solve it. thanks for the help
while ( choice != 'f' && choice != 'F' )
{ while ( inputFile >> num )
{
switch (choice)
{
case'A' :
while ( largenum < num )
largenum = num;
cout << "The largest value is " << largenum << "." << endl;
break;
case'a' :
while ( largenum < num )
largenum = num;
cout << "The largest value is " << largenum << "." << endl;
break;
case'B' :
while ( smallnum < num )
smallnum = num;
cout << "The smallest value is " << smallnum << "." << endl;
break;
case'b' :
while ( smallnum < num )
smallnum = num;
cout << "The smallest value is " << smallnum << "." << endl;
break;
case'C' :
sum += num;
break;
case'c' :
sum += num;
break;
case'D' :
counter++;
sum += num;
average = sum / counter;
cout << "The average value is " << average << "." << endl;
break;
case'd' :
counter++;
sum += num;
average = sum / counter;
cout << "The average value is " << average << "." << endl;
break;
case'E' :
counter++;
cout << "The number of values entered is " << counter << "." << endl;
break;
case'e' :
counter++;
cout << "The number of values entered is " << counter << "." << endl;
break;
case'F' :
cout << "Program Ending\n" << endl;
cout << "Press Enter to end -->" << endl;
cin.get();
break;
case'f' :
cout << "Program Ending\n" << endl;
cout << "Press Enter to end -->" << endl;
cin.get();
break;
default :
cout << choice << " is an invalid value." << endl;
}
}
Im having trouble starting or building this program for my c++ class pease help someone.
The program i need to run needs to open a file that contains a series of numbers reaads all the numbers from the file and calculates the following:
A) The number of numbers in the file
B) The sum of all the numbers in the file ( a running total )
Your code is hard to read... make sure to use the code formatting when posting code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
int da_getElement (int * a, int n, int i){
int j=0;
if (i<=n){
while (j<(n-1)){
a=a+j;
if (a=&a[i]){
return a[i];
}
j++;
}
}
else {
cerr<<"the index you entered is larger than the size of the array"<<endl;
}
return 0;
}
You have indescript variable names, no indentation, and no comments. It is difficult to figure out what this code is supposed to do. After adding some indentation and changing your variable names, some of the problems become more evident:
int da_getElement (int * theArray, int arraySize, int index)
{
int j=0;
if (index<=arraySize)
{
while (j<(arraySize-1))
{
theArray=theArray+j;
if (theArray=&theArray[index])
{
return theArray[index];
}
j++;
}
}
else
{
cerr<<"the index you entered is larger than the size of the array"<<endl;
}
return 0;
}
Lines 8 and 9 are nothing but trouble. I doubt you want to redefine the starting point of your array. I think that is what your 'j' variable is for: moving over the array. Can you describe more about your problem and what your goal is?
Well, then, the code simply need to check the two conditions (0<i) and (i<n) with an if statement, and do one of the two actions, either return a[i] or issue the error - and return some default value such as 0 or -1.