# Thread: array calculation error need help

1. ## array calculation error need help

hello im having problem with the program question below, it compiles but the calculations are giving wrong values, the program is reading in 10 integers from the user and adding one to the first value two to the second value and three to the third value and so on...... and the second part is de encrypting i would be grateful if someone can figure out what i am doing wrong thanks

Code:
```#include<stdio.h>
main(){
int numb[10],i;
int choice;

printf("What would you like to do?\n");
printf("1. Encrypt a string \n");
printf("2. De-encrypt a string\n");
scanf("%d",&choice);

switch(choice)

case 1:
{
printf("Enter your 10 values to encrypt: \n");
for(i=0;i<9;i++)
scanf("%d\n",&numb[i]);
printf("\n");

printf("Encrypted numbers are: \n");

for(i=0;i<9;i++){
printf("%d\n",numb[i]);
numb[0]=numb[0]+1;
numb[1]=numb[1]+2;
numb[2]=numb[2]+3;
numb[3]=numb[3]+4;
numb[4]=numb[4]+5;
numb[5]=numb[5]+6;
numb[6]=numb[6]+7;
numb[7]=numb[7]+8;
numb[8]=numb[8]+9;
numb[9]=numb[10]+10;

}

case 2:
printf("Enter your 10 values to De-encrypt: \n");
for(i=0;i<9;i++)
scanf("%d\n",&numb[i]);
printf("\n");

printf("De-encrypted numbers are: \n");

for(i=0;i<9;i++){
printf("%d\n",numb[i]);
numb[0]=numb[0]-1;
numb[1]=numb[1]-2;
numb[2]=numb[2]-3;
numb[3]=numb[3]-4;
numb[4]=numb[4]-5;
numb[5]=numb[5]-6;
numb[6]=numb[6]-7;
numb[7]=numb[7]-8;
numb[8]=numb[8]-9;
numb[9]=numb[9]-10;

}

}
}```

2. Look at line 33.

In fact, use a loop to do the work - like you seem to be trying to do.

Don't just copy/paste lines, make edits (and mistakes).

3. for (i=0; i<=9 ; i++)
numb[i]=numb[i]-(i+1);
for(i=0;i<=9;i++)
printf("%d\n",numb[i]);

try this instead of for loop in 45

4. thanks i just corrected line 33 but i still get wrong calculation say i put 0 for all the integers its should be
0 0 0 0 0 0 0 0 0 0 input entered by user
1 2 3 4 5 6 7 8 9 10 what the output should be

0 2 6 12 20 30 42 56 72 this is the output i am getting

5. thanks zebani your loop is working much appreciated

6. Then maybe you should look at your loop construct.
Code:
`for(i=0;i<9;i++)`
How many times do you think this loop will execute? (Hint it is not 10).

Jim

7. also you should have a break statement at the bottom of each case segment

Popular pages Recent additions