minor integer or array problems

This is a discussion on minor integer or array problems within the C Programming forums, part of the General Programming Boards category; I cant seems to get the answer of the program below, I try to create a program where the user ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    27

    minor integer or array problems

    I cant seems to get the answer of the program below, I try to create a program where the user input 10 numbers and the first 5 numbers in the arrays will then deduct with the next last 5 numbers in the arrays.

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<ctype.h>
    #include<stdlib.h>
    #include<string.h>
    
    void Difference();
    
    main(){
    Difference();
    fflush(stdin);
    return 0;
    }
    
    void Difference(){
    int intarr[10];
    int i;
    int answer;
    do
    {
    printf("Please enter the number :");
    scanf("%d",&intarr[i]);
    i++;
    }
    while(i<10);
    answer=(intarr[0]+intarr[1]+intarr[2]+intarr[3]+intarr[4])-(intarr[5]+intarr[6]+intarr[7]+intarr[8]+intarr[9]);
    printf("Your answer", answer);
    getch();
    clrscr();
    }

  2. #2
    Registered User
    Join Date
    Mar 2005
    Posts
    27
    When I run, the answer cannot be displayed

  3. #3
    Registered User dinjas's Avatar
    Join Date
    Feb 2005
    Location
    Vancouver, Washington
    Posts
    40
    you don't assign an initial value to i so it doesn't know what its value is in your do-while loop
    Code:
    int i;
    there's no format specifier for printf
    Code:
    printf("Your answer", answer);
    Last edited by dinjas; 03-11-2005 at 02:56 AM. Reason: correct description
    straight off the heap

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,812
    1: main should be int main

    2:Never fflush(stdin) it is undefined
    Woop?

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    27
    [QUOTE=prog-bman]1: main should be int main

    sorry but i dont quite get it you mean the main function can be insert with int?

  6. #6
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,812
    I mean that main should be just as I said int main eg.
    Code:
    int main(void)
    {
      /*Your code here*/
      return 0;
    }
    Woop?

  7. #7
    Registered User
    Join Date
    Mar 2005
    Posts
    27
    i've tried, everything is in order but still the answer cannot be displayed. it accept input and so on just the answer is blank

  8. #8
    Registered User
    Join Date
    Mar 2005
    Posts
    27
    Is there a problem with the logic?

  9. #9
    Registered User
    Join Date
    Mar 2005
    Posts
    9
    Logic is ok.

    main should always return an int

    For any function, not just main, if no arguments are needed, should be given the void parameter.


    As said previously, you need to initialise i, otherwise the expression i++, will add one to a garbage value.
    Code:
    int i;
    Code:
    do
    {
    printf("Please enter the number :");
    scanf("%d",&intarr[i]);
    i++;
    }
    while(i<10);
    Also, the printf statement that prints the value of answer, doesn't know what format answer is in (int, float, hex, octal, char, string etc). So you should really specify a format. EG:

    Code:
    printf("%d\n", answer);
    Note that %d tells printf to print answer as a signed decimal. You might wanna check the web for function explanations.

    Here's a link for the FAQ entry.

  10. #10
    Registered User
    Join Date
    Mar 2005
    Posts
    27
    thanks for your help the last one help me out...Thanks

  11. #11
    Registered User
    Join Date
    Mar 2005
    Posts
    9
    what they were trying to get at with the int i; followed by i++; is that it will just use the value that was in that space of memory befor it was allocated as i. This can cause errors in your results. you have to set i to equal something (like the value you want it to start at) befor you use it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 05:48 PM
  2. Have problems with copying my array!
    By AvaGodess in forum C Programming
    Replies: 11
    Last Post: 09-25-2008, 01:56 AM
  3. Problems passing an array to a function
    By ndenklau in forum C Programming
    Replies: 5
    Last Post: 09-20-2006, 09:14 AM
  4. Helllppp!!! I/O and array problems
    By dorky in forum C++ Programming
    Replies: 3
    Last Post: 07-02-2005, 10:24 AM
  5. Creating 2D arrays on heap
    By sundeeptuteja in forum C++ Programming
    Replies: 6
    Last Post: 08-16-2002, 12:44 PM

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