### trouble knowing the problem in my code

hey guys, i wrote this code this is part of it:

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
can you help me with mine and ill get help for yours?

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364`` ``````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 )

C) The Average of all the numbers in the file

And displays all of these.

File name TopicD.cpp
i am really sorry am still a beginner and i am struggling too, once i know anything i will inform you
Your code is hard to read... make sure to use the code formatting when posting code:

 ``1234567891011121314151617`` ``````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"<
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:
 ``12345678910111213141516171819202122`` ``````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"<

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?
Last edited on
the assignment is to use this function which will return the value of i if 0<i<n, otherwise it issues an error.
 the assignment is to use this function which will return the value of i if 0

So the array doesn't play any role at all?
i think it is supposed to return a[i]. btw thank you all :)
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.
Last edited on
thank you :)
Topic archived. No new replies allowed.