Thread: Need help with two programs please!

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    3

    Need help with two programs please!

    The first program was to read in a list of decimal numbers, as doubles, computing sum and mean. The list should be n, x_1, x_2..........x_n. I have managed to write the following program but i don't know to insert data, as in 7 numbers for it to compute.
    Code:
    #include <stdio.h>
    
    main()
    {
           int i, n;
           double x, sum, mean;
           scanf ("&#37;d", &n);
           
           for (i=0; i<n; ++i)
           {
                scanf ("%lf", &x);
                sum = sum + x;
                mean = sum % n
           }
           printf("n= %d, sum= %f, mean= %f", n, sum, mean);
    }
    The second program, i am seriously struggling with and have no idea about how to do it so any help would be greatly appreciated.

    PRSI is estimated according to the following rules. (Amounts in euros.)

    < 165 = 0.5%
    165 -- 1300 = 8.5 %
    > 1300 = 10.75 %

    Write a program to read a euro amount from the command line and print the amount before and after PRSI is added.
    For example,

    % a.out 1.23
    1.23 plus PRSI is 1.24
    % a.out 100
    100.00 plus PRSI is 100.50
    % a.out 164.99
    164.99 plus PRSI is 165.81
    % a.out 165
    165.00 plus PRSI is 179.02
    % a.out 1300
    1300.00 plus PRSI is 1410.50
    % a.out 1300.01
    1300.01 plus PRSI is 1439.76
    %
    Last edited by healyal; 11-02-2007 at 05:53 PM.

  2. #2
    Registered User
    Join Date
    Jan 2007
    Location
    Euless, TX
    Posts
    144
    For your first program, do you know the 'n' value or are you supposed to take as many values as the user wants to give you? If the is no 'n', then you just read in the value and add it to a variable 'sum', keep a 'counter for all the numbers added, and figure out your mean value, right? If you know there are 'n' values coming in, then you can use the loop if you want with the same logic.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    3
    For the first program n is meant to be equal to 7. The program was to be written for the list n, x_1.......x_n and then tested with the data 7,1,1,2,4,5,3,7 and was to give an output of
    n 7 sum 23.000000 mean 3.285714.

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Code:
    scanf ("%1f", &x);
    That's not right; the conversion in scanf() for a double is %lf. That is, the letter "ell" and then f. Not the number one. If you're using gcc, the -Wall flag will warn you about that. If not, find out how to turn up the warning level on your compiler. You then might see a warning about too many arguments passed for your last printf(). Notice how you have &f instead of %f.

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    3
    Thanks for the correction on the %1f to %lf, as for the &f that was just a typo.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recommend upgrade path for C programs
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-22-2007, 07:32 AM
  2. Newbie question: pointers, other program's memory
    By xxxxme in forum C++ Programming
    Replies: 23
    Last Post: 11-25-2006, 01:00 PM
  3. Problem using java programs within C code
    By lemania in forum Linux Programming
    Replies: 1
    Last Post: 05-08-2005, 02:02 AM
  4. POSIX/DOS programs?
    By nickname_changed in forum C++ Programming
    Replies: 1
    Last Post: 02-28-2003, 05:42 AM
  5. executing c++ programs on the web
    By gulti01 in forum C++ Programming
    Replies: 4
    Last Post: 08-12-2002, 03:12 AM