Like Tree1Likes
  • 1 Post By jimblumberg

array calculation error need help

This is a discussion on array calculation error need help within the C Programming forums, part of the General Programming Boards category; hello im having problem with the program question below, it compiles but the calculations are giving wrong values, the program ...

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    6

    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. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,531
    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).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    5
    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
    Last edited by zebani; 12-28-2011 at 10:41 AM.

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    6
    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. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    6
    thanks zebani your loop is working much appreciated
    Last edited by Serj; 12-28-2011 at 10:54 AM.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    2,740
    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
    FloridaJo likes this.

  7. #7
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    594
    also you should have a break statement at the bottom of each case segment

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Standard Deviation Calculation Error?
    By faulerwulf in forum C Programming
    Replies: 15
    Last Post: 12-01-2011, 01:39 AM
  2. Please Help ASAP!!! Calculation error!
    By JSM in forum C Programming
    Replies: 3
    Last Post: 11-08-2010, 04:46 AM
  3. Average Calculation Error
    By BamaMarine06 in forum C Programming
    Replies: 2
    Last Post: 10-04-2010, 08:13 PM
  4. source of calculation error?
    By stanlvw in forum C++ Programming
    Replies: 11
    Last Post: 08-12-2004, 05:04 PM
  5. Arrays/calculation error... *need Help*
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 03-05-2002, 11:45 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21