0

How do you explain the following function?

Please help me understand how the last line in the read () function in cpp below does not let the 0 to be counted? void read (int a[], int& n); cout<< “Enter integers. Terminate with 0: \n”; n=0; do{ cout<< “a[“<<n<<“]: “; cin >> a[n]; } while (a[n++] !=0 && n<100 ); - - n;

3rd Dec 2019, 5:59 PM
Jack
7 ответов
+ 1
It is perfectly working... If you want to print last 0 also , then just remove n--; In read function... https://code.sololearn.com/cX0nY2yPVr5e/?ref=app here you are taking size variable reference as argument so changes in the function affects original value.
3rd Dec 2019, 6:25 PM
Jayakrishna 🇮🇳
0
Its better to show full code.. Or run it in the play ground and share link here.. Are you taking about a[n++]!=0 If yes, some times it depends on the compiler. So specify clearly..
3rd Dec 2019, 6:10 PM
Jayakrishna 🇮🇳
0
what is the purpose of the read() function?
3rd Dec 2019, 6:11 PM
Bahhaⵣ
Bahhaⵣ - avatar
0
The entire code is: #include <iostream> using namespace std; void read(int [], int&); void print( int [], int); long sum (int [], int); const int MAXSIZE=100; int main(){ int a[MAXSIZE]={0}, size; read (a,size); cout << "The array has " <<size <<" elements: "; print (a,size); } void read(int a[], int& n){ cout <<"Enter integers. Terminate with 0: \n"; n=0; do{ cout << "a ["<<n<<"]: "; cin >> a[n]; } while (a[n++] !=0 && n<MAXSIZE); --n; } void print (int a[], int n){ for (int i=0; i<n; i++) cout <<a[i]<<" "; cout<<endl; }
3rd Dec 2019, 6:13 PM
Jack
0
you have a | do{} while | loop in your code, that's the problem, it will not work in playground, because it's not a real terminal. it will not wait for you to enter 0 to terminate. try your code on a computer.
3rd Dec 2019, 6:21 PM
Bahhaⵣ
Bahhaⵣ - avatar
0
@ Jaya Krishna Thanks. Sorry but I think I have not asked my question clearly. I want to know how the line (--n) prevents the reading of 0. As an explanation for this line I have (//don't count the 0).
3rd Dec 2019, 6:29 PM
Jack
0
Jack Its ok. Fine... And Wel come
3rd Dec 2019, 6:32 PM
Jayakrishna 🇮🇳