C++ looping

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

QuestionEdit

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[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.

  1. include<iostream.h>
  2. 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 

AnswerEdit

> 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] ;


QuestionEdit

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[1]=A[1]+B[1].

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[1]=A[1]+B[1].

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

void main() { int i,j,k,a[5],b[5],c[5]; 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(); }

I hope you will please answer.

                                regards,
                                Prakash Kumar

AnswerEdit

The first element of an array has the subscript zero (0). The expression a[4] refers to the last element in an array containing five elements. For an array containing N elements, the valid subscripts are: 0, 1, 2, .... N-1.

So your loops should be: for( i=0 ; i<5 ; ++i )

Advertisement

©2024 eLuminary LLC. All rights reserved.