Need help with array program.

This is a discussion on Need help with array program. within the C Programming forums, part of the General Programming Boards category; I'm writing a program using an array. Write a program that asks for user input of two integers, write the ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    10

    Need help with array program.

    I'm writing a program using an array.

    Write a program that asks for user input of two integers, write the code so that the program does the sum of two integers, the subtraction of two integers, the division of two integers, the multiplication of two integers and stores the results in an array and outputs the results of that array to the screen.

    My program seems to work, but I'm wondering if I did the array part right. Also, when I enter -1 to end it makes me enter another number before it will close out the program. Not sure why this is.

    Code:
    #include <stdio.h>
    int main ( void )
    {
            int x;
            int y;
            float array[4];
    
            printf( "Enter two integers: \n" );
            scanf( "%d" "%d",&x, &y );
    
            while( x != -1 )  
            {
                array[0] = x + y;
                array[1] = x - y;
                array[2] = x * y;
                array[3] = x / ( float )y;
    
                printf( "The results of the sum of the two integers is: %.2f\n", array[0] );
                printf( "The results of the subtraction of the two integers is: %.2f\n", array[1] );
                printf( "The results of the multiplication of two integers is: %.2f\n", array[2] );
                printf( "The results of the division of two integers is: %.2f\n", array[3] );
    
                printf( "Enter two integers: \n" );
                scanf( "%d" "%d",&x, &y );
            }
    
    return 0;
    }

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    The array seems to be okay.The reason you get the program to aks for two numbers to stop executing is that scanf into the body loop requires two numbers to be typed.If you want to be done with one,then you should read them seperately.

    Also please note that you use a float array for handling int input.Why?
    If the user tries to input a float number then i guess the program won't work

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    scanf() for just ONE number inside the loop. Then, test that one number, and if it == -1, then your loop should end, if it's anything else, your loop should continue.

  4. #4
    Registered User
    Join Date
    Oct 2012
    Posts
    10
    I used the float because of the division portion. 15 / 2 gave me 7 rather than 7.5.

    I'm still not quite sure how to end on 1 input. I tried:

    Code:
    scanf( "%d",&x );
    scanf( "%d",&y );
    But that does the same thing. I also tried only scanning for x which worked, but if I didn't wan to end then it only reads 1 number.
    I'm probably missing something obvious or just not understanding what you mean by testing separately.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,307
    Hint: You need to put some code between the two scanf's.

    Note that it would be better to put the part that asks for input from the user at the top of the loop, so that doesn't have to be in two places in your program.
    Last edited by iMalc; 10-21-2012 at 02:52 PM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. array program help.
    By chumpp in forum C Programming
    Replies: 3
    Last Post: 07-26-2012, 02:32 AM
  2. Need help with a array program
    By antim in forum C Programming
    Replies: 2
    Last Post: 03-06-2011, 12:16 PM
  3. Array Program in C
    By Buddha in forum C Programming
    Replies: 3
    Last Post: 06-05-2010, 07:05 PM
  4. Replies: 1
    Last Post: 03-16-2010, 10:17 AM
  5. Help converting array program to link list program
    By hsmith1976 in forum C++ Programming
    Replies: 0
    Last Post: 02-14-2010, 08:50 PM

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