C++/c++ looping
Expert: vijayan - 12/12/2010
QuestionSir, 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[1]=A[1]+B[5]
C[2]=A[2]+B[4]
..
..
C[5]=A[5]+B[1].
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.
#include<iostream.h>
#include<conio.h>
void main()
{
int i,j,k,a[4],b[4],c[4];
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
Answer> find the sum of the array such that C[1]=A[1]+B[5]
> C[2]=A[2]+B[4]
I presume that you mean, with N as the number of elements, is:
C[0] = A[0] + B[N-1] ;
C[1] = A[1] + B[N-2] ;
...
...
C[N-2] = A[N-2] + B[1] ;
C[N-1] = A[N-1] + B[0] ;
A single loop would suffice.
for( int i=0 ; i<N ; ++i ) c[i] = a[i] + b[N-i-1] ;