Arithmetic Operations and formatting help!

This is a discussion on Arithmetic Operations and formatting help! within the C Programming forums, part of the General Programming Boards category; I am supposed to write a code for my very first assignment where when the user enters four integers the ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    9

    Arithmetic Operations and formatting help!

    I am supposed to write a code for my very first assignment where when the user enters four integers the program would perform several operations on it.

    The problem I am having is that the product and ratio are incorrect.

    Also, I cannot figure out how to get the remainder to appear.

    Formatting issues I have is I do not know how to make a colon appear after the words. For example,

    Please enter four integers:

    The colon doesn't show up when it runs.

    I also cannot get the strings to skip a line like,

    Please enter four integers: 1 2 3 4

    Sum is: 10

    This is my code, please help me out!

    Code:
    /*------------- Include Section --------------*/ #include <stdio.h> #include <stdlib.h> #include <limits.h> /*------------------Main Functions------------------ Purpose: Allow user to input four different integers to have the programme give multiple outputs of different arithmatic operations executed on these integers. Returns: Sum, product, average, smallest, second-smallest, ration between smallest and second-smallest. --------------------------------------------------*/ int main(void) { int num1; int num2; int num3; int num4; int sum; int product; int avg; float avgrem; int small; int small2; int ratio; float ratiorem; printf("Please enter four different integers:"); scanf("%d %d %d %d", &num1, &num2, &num3, &num4); sum = num1 + num2 + num3 + num4; product = num1 * num2 * num3 * num4; avg = product / 4.0; avgrem = product % 4.0; small = num1; //To assign smallest number to int small if (small > num2) small = num2; if (small > num3) small = num3; if (small > num4) small = num4; small2 = INT_MAX; /*To assign second smallest number to small2. Assume small2 has the max value of type int*/ if (small == num1) { if (small2 > num2) small2 = num2; if (small2 > num3) small2 = num3; if (small2 > num4) small2 = num4; } if (small == num2) { if (small2 > num1) small2 = num1; if (small2 > num3) small2 = num3; if (small2 > num4) small2 = num4; } if (small == num3) { if (small2 > num1) small2 = num1; if (small2 > num2) small2 = num2; if (small2 > num4) small2 = num4; } if (small == num4) { if (small2 > num1) small2 = num1; if (small2 > num2) small2 = num2; if (small2 > num3) small2 = num3; } ratio = small2 / small; ratiorem = small2 % small; printf("Sum is %d\n", sum); printf(" \n"); printf("Product is %d\n", product); printf("Average is %d%f\n", avg, avgrem); printf("Second Smallest is %d\n", small2); printf("Smallest is %d\n", small); printf("Ratio between Second Smallest and Smallest is %d%f\n", ratio, rationrem); printf("**** Program Terminated ****"); return (0); }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok... not bad for a first assignment...

    avg is defined as an int... int's do not have remainders, only whole numbers.
    acgrem is a float... the % operator only works on ints.

    The colon is not showing up because there's no colon in your printf() format strings.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    9
    Quote Originally Posted by CommonTater View Post
    Ok... not bad for a first assignment...

    avg is defined as an int... int's do not have remainders, only whole numbers.
    acgrem is a float... the % operator only works on ints.

    The colon is not showing up because there's no colon in your printf() format strings.
    Thanks for your reply.

    So if I define avg as a float does that fix it, and if its defined as a float do I still need the remainder?

    I put colons there and they do not show up regardless.

    Would the fact that I'm compiling in Code::Blocks mean anything?

    New Code:

    Code:
    /*-------------
    Include Section
    --------------*/
    #include <stdio.h>
    #include <stdlib.h>
    #include <limits.h>
    
    
    /*------------------Main Functions------------------
    Purpose: Allow user to input four different integers to have the programme give multiple outputs of different arithmatic operations executed on these integers.
    Returns: Sum, product, average, smallest, second-smallest, ration between smallest and second-smallest.
    --------------------------------------------------*/
    
    
    int
    main(void)
    {
        int num1;
        int num2;
        int num3;
        int num4;
        int sum;
        int product;
        int avg;
        float avgrem;
        int small;
        int small2;
        int ratio;
        float ratiorem;
    
    
        printf("Please enter four different integers:");
        scanf("%d %d %d %d", &num1, &num2, &num3, &num4);
    
    
        sum = num1 + num2 + num3 + num4;
        product = num1 * num2 * num3 * num4;
        avg = product / 4.0;
        avgrem = product % 4.0;
        small = num1; //To assign smallest number to int small
            if (small > num2) small = num2;
            if (small > num3) small = num3;
            if (small > num4) small = num4;
    
    
        small2 = INT_MAX; /*To assign second smallest number to small2. Assume small2 has the max value of type int*/
            if (small == num1) {
                if (small2 > num2) small2 = num2;
                if (small2 > num3) small2 = num3;
                if (small2 > num4) small2 = num4;
            }
    
    
            if (small == num2) {
                if (small2 > num1) small2 = num1;
                if (small2 > num3) small2 = num3;
                if (small2 > num4) small2 = num4;
            }
    
    
            if (small == num3) {
                if (small2 > num1) small2 = num1;
                if (small2 > num2) small2 = num2;
                if (small2 > num4) small2 = num4;
            }
    
    
            if (small == num4) {
                if (small2 > num1) small2 = num1;
                if (small2 > num2) small2 = num2;
                if (small2 > num3) small2 = num3;
            }
    
    
        ratio = small2 / small;
        ratiorem = small2 % small;
    
    
        printf("Sum is %d\n", sum);
        printf(" \n");
        printf("Product is %d\n", product);
        printf("Average is %d%f\n", avg, avgrem);
        printf("Second Smallest is %d\n", small2);
        printf("Smallest is %d\n", small);
        printf("Ratio between Second Smallest and Smallest is %d%f\n", ratio, rationrem);
        printf("**** Program Terminated ****");
    
    
        return (0);
    }

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Take a look at this and apply it to the rest of your program:
    Code:
    #include <stdio.h>
    
    int main(void){
    
    	int num1,num2,num3,num4,sum, product; //note same type can be define on same line
    	float average; //average as float so you have decimal value
    
    	printf("Enter 4 numbers: ");
    	scanf("%d %d %d %d",&num1,&num2,&num3,&num4);
    
    	sum = num1+num2+num3+num4;
    	product = num1*num2*num3*num4;
    	/*note the divisor, the 'f' stands for float*/
    	average = sum / 4.0f;
    
    	/*Note in the printf statements there are ':' at the end*/
    	printf("Sum: %d\n",sum);
    	printf("Average: %f\n",average);
    	printf("Product: %d\n",product);
    
    	return(0);
    }
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. arithmetic operations
    By XodoX in forum C++ Programming
    Replies: 6
    Last Post: 06-29-2009, 01:28 AM
  2. Arithmetic Help
    By cduce2411 in forum C++ Programming
    Replies: 3
    Last Post: 03-18-2009, 12:58 PM
  3. arithmetic help
    By freeindy in forum C Programming
    Replies: 10
    Last Post: 09-18-2007, 09:01 AM
  4. Resolve Arithmetic operations
    By louis_mine in forum C Programming
    Replies: 3
    Last Post: 11-13-2006, 09:14 PM
  5. doing floating operations using integer operations
    By ammalik in forum C Programming
    Replies: 10
    Last Post: 08-15-2006, 05:30 AM

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