variable declarations and arithmetic expressions

This is a discussion on variable declarations and arithmetic expressions within the C Programming forums, part of the General Programming Boards category; ask the user for 4 integer values 5. The quotient (real, with decimals) of the square root of the sum ...

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

    variable declarations and arithmetic expressions

    ask the user for 4 integer values

    5. The quotient (real, with decimals) of the square root of the
    sum of the squares
    divided by the sum of all the numbers.

    the program runs all other operations expect #5.

    How can i fix it?

    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    int main()
    {
        
        int num[4];
        
        printf("Enter 4 numbers seperated by a space in between each:\n");
        scanf("%d %d %d %d",&num[0],&num[1],&num[2],&num[3]);
        
        printf("The sum of the 4 numbers is : %d\n", num[0]+num[1]+num[2]+num[3]);
        printf("The sum of the first two numbers minus the sum of the last two is : %d\n", (num[0]+num[1])-(num[2]+num[3]));
        printf("The sum of the squares of the four numbers is : %d\n", (num[0]*num[0]+num[1]*num[1]+ num[2]*num[2]+num[3]*num[3]));
        printf("The quotient of the square root of the sum of the squares divided by the sum of all the numbers is : %f\n", sqrt(num[0]*num[0]+num[1]*num[1]+ num[2]*num[2]+num[3]*num[3])/(num[0]+num[1]+num[2]+num[3]);
               
                                                                                                                                                                                            return (0);
                                                                                                                                                                                            }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,825
    What did you see?
    What did you expect?

    You can't just dump code and say "it doesn't work", without you saying what you expect from it (at the very least).
    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
    Oct 2012
    Posts
    19
    i found the problem. The program is working now.

  4. #4
    Registered User
    Join Date
    Oct 2012
    Posts
    19
    THE CORRECT PROGRAM

    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    int main(void)
    
    {
        
        int num[4];
        
        printf("Enter 4 numbers seperated by a space in between each:\n");
        
        scanf("%d %d %d %d",&num[0],&num[1],&num[2],&num[3]);
        
        printf("The sum of the 4 numbers is : %d\n", num[0]+num[1]+num[2]+num[3]);
        
        printf("The sum of the first two numbers minus the sum of the last two is : %d\n", (num[0]+num[1])-(num[2]+num[3]));
        
        printf("The sum of the squares of the four numbers is : %d\n", (num[0]*num[0]+num[1]*num[1]+ num[2]*num[2]+num[3]*num[3]));
        
        printf("The quotient of the square root of the sum of the squares divided by the sum of all the numbers is : %f\n",
               sqrt(num[0]*num[0]+num[1]*num[1]+ num[2]*num[2]+num[3]*num[3])/(num[0]+num[1]+num[2]+num[3]));
        
        return (0);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Variable Declarations
    By PritinTheGreat in forum C++ Programming
    Replies: 1
    Last Post: 06-05-2008, 03:49 AM
  2. Declarations
    By slippy in forum C++ Programming
    Replies: 13
    Last Post: 12-01-2007, 05:02 PM
  3. Stacks and arithmetic expressions
    By +Azazel+ in forum C Programming
    Replies: 11
    Last Post: 10-23-2007, 03:48 AM
  4. Replies: 1
    Last Post: 11-19-2001, 04:45 PM
  5. variable declarations
    By K&J in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2001, 11:28 AM

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