# C++ looping

Last Edited By Krjb Donovan
Last Updated: Mar 11, 2014 07:51 PM GMT

## Question

Sir, thanks for your last reply. However I have I more question of same looping type but is to be done is different. the question is :

WAP to enter two arrays of five elements each and find the sum of the array such that C=A+B C=A+B .. .. C=A+B. I was able to run the code twice and get the required output but its not working now I don't know why? I am posting the code below hope you will help.

1. include<iostream.h>
2. include<conio.h>

void main() {

int i,j,k,a,b,c; clrscr(); for(i=0;i<5;i++) { cout<<"Enter the "<<(i+1)<<" element of the array ONE : "; cin>>a[i]; } for(j=0;j<5;j++) { cout<<"Enter the "<<(j+1)<<" element of the array TWO : "; cin>>b[j]; } for(k=0;k<5;k++) { for(i=0;i<5;i++) { for(j=4;j>=0;j--) { c[k]=a[i]+b[j]; cout<<"The sum of array ONE "<<(i+1)<<" element and array TWO "<<(j)<<" is : "<<c[k]<<endl; break; } break; } } getch(); }

I am waiting for your response eagerly.

```                                     regards,
Prakash Kumar Singh
```

> find the sum of the array such that C=A+B > C=A+B

I presume that you mean, with N as the number of elements, is: C = A + B[N-1] ; C = A + B[N-2] ; ... ... C[N-2] = A[N-2] + B ; C[N-1] = A[N-1] + B ;

A single loop would suffice.

for( int i=0 ; i<N ; ++i ) c[i] = a[i] + b[N-i-1] ;

## Question

Sir, I have a question related to arrays and loop in c++ its a real simple question you will agree with me but the problem I am facing is with the results. The question is this: WAP to enter two arrays of five elements each and find the sum of the array such that C=A+B.

I get the results correct for first four value of both arrays that I enter but the fifth result is some garbage value.I am providing the code below hope you will look into it and please answer.

WAP to enter two arrays of five elements each and find the sum of the array such that C=A+B.

1. include<iostream.h>
2. include<conio.h>

void main() { int i,j,k,a,b,c; clrscr(); for(i=1;i<=5;i++) { cout<<"Enter the "<<(i)<<" element of the array ONE : "; cin>>a[i]; }

for(j=1;j<=5;j++) { cout<<"Enter the "<<(j)<<" element of the array TWO : "; cin>>b[j]; }

for(i=1;i<=5;i++) { for(j=i;j<=i;j++) { for(k=i;k<=i;k++) { c[k]=a[i]+b[j]; cout<<"The sum of array ONE "<<(i)<<" element and array TWO "<<(j)<<" is : "<<c[k]<<endl; } } } getch(); }

```                                regards,
Prakash Kumar
```