Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
July 25th, 2013, 04:05 AM
 Cantafford
Registered User

Join Date: Jun 2013
Posts: 23
Time spent in forums: 1 h 35 m 16 sec
Reputation Power: 0
Explain program line

Hello. I need to write a program which combines two arrays of n(<=20) elements by using the relations: a[i]=a[i]+b[i],
b[i]=a[i]*b[i].

Initially I wrote the program like this:

Code:
```
#include <stdio.h>
int main()
{
int n,a[20],b[20],i;
printf("Enter number of elements: "); scanf("%d", &n);
printf("Enter number of elements for a:\n");
for(i=0; i<n; i++)
scanf("%d", &a[i]);
printf("Enter number of elements for b:\n");
for(i=0; i<n; i++)
scanf("%d", &b[i]); // read arrays a and b

for(i=0; i<n; i++) // SECOND FOR
{
a[i]=a[i]+b[i];
b[i]=a[i]*b[i];
}

printf("Array a is:\n");
for(i=0; i<n; i++)
printf("%d ", a[i]);

printf("\nArray b is:\n");
for(i=0; i<n; i++)
printf("%d ", b[i]);

getch();
return 0;
}

```

But in my book says I need to store the a[i] into an aux variable in the first for otherwise the program won't function properly and I don't really understand why. Thank you for reading.

#2
July 25th, 2013, 04:09 AM
 Cantafford
Registered User

Join Date: Jun 2013
Posts: 23
Time spent in forums: 1 h 35 m 16 sec
Reputation Power: 0
I got it

#3
July 25th, 2013, 04:11 AM
 eramit2010
Contributing User

Join Date: Mar 2013
Location: India
Posts: 95
Time spent in forums: 2 Days 18 h 9 m 17 sec
Reputation Power: 3
Code:
``` a[i]=a[i]+b[i];
b[i]=a[i]*b[i]; ```

in first line u r changing value of a[i] to a[i]+b[i]
so when u will use a[i] in 2nd line at that time a[i] will be a[i]+b[i].
so to use it 2nd time u have to store a[i] in temp. variable.

or another solution is that use
Code:
`b[i]=(a[i]-b[i])*b[i]`

#4
July 25th, 2013, 04:13 AM
 eramit2010
Contributing User

Join Date: Mar 2013
Location: India
Posts: 95
Time spent in forums: 2 Days 18 h 9 m 17 sec
Reputation Power: 3
Quote:
 Originally Posted by Cantafford I got it

thats good that u got it by yourself

#5
July 25th, 2013, 10:59 AM
 Cantafford
Registered User

Join Date: Jun 2013
Posts: 23
Time spent in forums: 1 h 35 m 16 sec
Reputation Power: 0
Quote:
 Originally Posted by eramit2010 thats good that u got it by yourself

The book helped me cudos anyway.

 Viewing: Dev Shed Forums > Programming Languages > C Programming > Explain program line