English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

#include
using namespace std;
#define MAX 20

main(void)
{
int loop;
int hold[MAX];
int counter = 0;
char more;

while(1){
cout << "enter a num" ;
cin >> hold[counter]; \\explain me this. Why was counter but in an array form[] ********************
cout << "\nENTER MORE NUM (Y,N)";
cin >> more;
if(more == 'n'){
break;
}else{
counter++;
}

}
for(loop = 0; loop <= counter; loop++){
cout << "element in" << counter+1 << " is " << hold[loop]; \\ why was loop in a array[] in hold.
}
system("PAUSE");
return(0);

}

2006-12-31 12:51:22 · 3 answers · asked by Best Helper 4 in Computers & Internet Programming & Design

cin >> hold[counter]; \\explain me this. Why was counter but in an array form[]

2. cout << "element in" << counter+1 << " is " << hold[loop]; \\why was loop put in a array form inside hold.

2006-12-31 12:52:30 · update #1

3 answers

cin >> hold[counter]; \\explain me this. Why was counter but in an array form[]

this is assigning the stuff you are typing into the hold array variable at the index of "counter" (which is incrementing each time the while loop iterates)

2. cout << "element in" << counter+1 << " is " << hold[loop]; \\why was loop put in a array form inside hold.

this is outputting the hold array at the index of "loop" while the for loop increments loop until loop is less than or equal to the counter you generated in the while loop.

the while loop iterates until you Type N. As it loops it keeps incrementing "counter" up and indexing the hold array by "counter" for your input to be stored in...

hold[0] = "your input here"
hold[1] = "your second input here"

then the for loop takes the counter and uses it as it's maximum and loops using "loop" as the iterator and as the index for the hold array.

printing out "your input here"
then "your second input here"



make sense?

2006-12-31 12:56:24 · answer #1 · answered by Jhered 3 · 0 0

this program accepts integer in an array hold which can hold maximum of 20 elements. counter is used as index which is incremented each time when there are more elements to enter. when finished the loop exits. the next for loop is used to display the elements entered. Always the arrays are used with there indexes which shows the elements offset. hold[0],hold[1]....hold[20] are the elements if array hold.

2007-01-01 04:00:58 · answer #2 · answered by Anonymous · 0 0

Someone else has already explained the code.

What I would consider more appropriate for C++:

#include
#include
#include
using namespace std;

int main()
{

vector nums;
string answer = "y";
int number = 0;
while(answer == "y")
{
cout << "Enter a number".
cin >> number; nums.push_back(number);
cout << "Another number (y/n) ?"
cin >> answer;
}

for(int i = 0; i < nums.size(); ++i)
{
cout << "Element in " << i << " is " << nums[i] << endl;
}

return 0;

}

2006-12-31 21:06:20 · answer #3 · answered by csanon 6 · 0 0

fedest.com, questions and answers