Thread: Need help With c Progam

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    19

    Need help With c Progam

    My computer just crashed and I lost my files, I have this program due tomorrow, if anyone could solve and show the code that'd be great.

    Problem below:------------------------------------------------

    Write a function, named sums(), that has two input parameters; an array, called Input of doubles; and an integer which is the number of values stored in the array. Compute the sum of the positive values in the array and the sum of the negative values. Also count the number of values in each category. Return these four answers through output parameters. Write a main program that reads no more than 10 real numbers and stores them in an array. Stop reading numbers when a 0 is entered. Call the sums() function and print the answers it returns. Also compute and print the average values of the positive and negative sets. Align decimal points on numbers
    SAMPLE INPUT:
    -123.45
    -234.56
    576.1
    -9.345
    675.2
    100
    -10
    1654.45
    765.89
    0 (NOT in computation)

    SAMPLE OUTPUT:
    YourName Program#1 CS110
    Input Read:
    9999.9999
    9999.9999
    ...

    Statistics:
    Desc Number Total: Average:
    Positive 99 99999.9999 9999.9999

    Negative 99 99999.9999 9999.9999
    Overall 99 99999.9999 9999.9999

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Fortunately, you have some time. Start it off -- and post it up, ASAP.

  3. #3
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    My computer just crashed and I lost my files...
    Sounds legit - We better do their homework for them!
    Fact - Beethoven wrote his first symphony in C

  4. #4
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    My computer just crashed and I lost my files
    So forget about the program for now and focus on recovering your files.
    TestDisk - CGSecurity

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Or better yet ->

    Dear Professor Foo -

    My computer has just crashed and I've lost my file for project Banana.

    My program took three days to complete, so I was hoping that I'd be able to get a 3 day extention to recomplete it.

    Kind regards,
    Student Foo.
    Fact - Beethoven wrote his first symphony in C

  6. #6
    Registered User
    Join Date
    Apr 2013
    Posts
    19
    Quote Originally Posted by Adak View Post
    Fortunately, you have some time. Start it off -- and post it up, ASAP.
    sorry thought I added it, here is what I got

  7. #7
    Registered User
    Join Date
    Apr 2013
    Posts
    19
    Code:
    #include <stdio.h>
     
    void sums(double Input[], int elements, double *PSum, double *NSum, int *numOfP, int *numOfN);
     
    int main()
    {
        double Input[10];
        double PSum = 0, NSum = 0, userInput; //positive and negative sum
        int numOfP = 0, numOfN = 0; //number of postive and negatives
        int i;
        for(i = 0; i < 10; i++){
              printf("Enter a real number or '0' to stop: ");
              scanf( " %lf", &userInput);
              if(userInput == 0){
                  break;
              }
              Input[i] = userInput;
        }
        sums(Input, i, &PSum, &NSum, &numOfP, &numOfN);
        printf("Sum of positives: %.2f\nNumber of positives: %d\n", PSum, numOfP);
        printf("Sum of negatives: %.2f\nNumber of negatives: %d\n", NSum, numOfN);
        printf("Average of positives: %.2f\n", PSum / numOfP);
        printf("Average of negatives: %.2f\n", NSum / numOfN);
     
        return 0;
        
    }
     
     
    void sums(double Input[], int elements, double *PSum, double *NSum, int *numOfP, int *numOfN)
    {
         int i;
         for(i = 0; i < elements; i++){
               if(Input[i] > 0){
                           *numOfP += 1;
                           *PSum += Input[i];
               }
               else{
                           *numOfN += 1;
                           *NSum += Input[i];
               }
         }
    }
    Last edited by craff25; 04-24-2013 at 09:03 PM.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Post your code in [code][/code] bbcode tags. Do not add any syntax highlighting of your own.

    I have edited your post for you.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Apr 2013
    Posts
    19
    Quote Originally Posted by Click_here View Post
    Sounds legit - We better do their homework for them!
    I apologize i thought i added the code didnt realize you needed to actually write in [code] on post i have uploaded what I've done so far

  10. #10
    Registered User
    Join Date
    Apr 2013
    Posts
    19
    Quote Originally Posted by laserlight View Post
    Post your code in [code][/code] bbcode tags. Do not add any syntax highlighting of your own.

    I have edited your post for you.
    I think thats right sorry im new to all this lol

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I understand this to mean the sum() function can only have two input parameters - you have several pointers extra. Please explain.

    Write a function, named sums(), that has two input parameters; an array, called Input of doubles; and an integer which is the number of values stored in the array.
    If the function returned the count of positive numbers, then you could calculate the count of negative numbers, using subtraction, in main() correct?
    Last edited by Adak; 04-25-2013 at 12:36 AM.

  12. #12
    Registered User
    Join Date
    Apr 2013
    Posts
    19
    Quote Originally Posted by Adak View Post
    I understand this to mean the sum() function can only have two input parameters - you have several pointers extra. Please explain.



    If the function returned the count of positive numbers, then you could calculate the count of negative numbers, using subtraction, in main() correct?
    yes i believe so

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    One way to do what you need to do, is to use a static variable, in sum(). In this case, a static variable of a struct, which you describe above main() (so that description is in global space, and any function can use it.

    I'm not sure this works OK, or not, and I'm not sure you can use a static variable or not.

    Code:
    #include <stdio.h>
      
    
    typedef struct stats{
       int posCount;
       int negCount;
       double posSum;
       double negSum;
    }stats;
    
    stats sums(double Input[], int elements);
      
    int main()
    {
       double Input[10];
       double userInput; //positive and negative sum
       stats stat;
       int i;
       for(i = 0; i < 10; i++){
          printf("Enter a real number or '0' to stop: ");
          scanf( " %lf", &userInput);
          if(userInput == 0){
             break;
          }
          Input[i] = userInput;
       }
       stat=sums(Input, i);
       printf("Sum of positives: %.2f\nNumber of positives: %d\n", stat.posSum,stat.posCount);
       printf("Sum of negatives: %.2f\nNumber of negatives: %d\n", stat.negSum,stat.negCount);
       printf("Average of positives: %.2f\n", (double)stat.posSum / i);
       printf("Average of negatives: %.2f\n", (double)stat.negSum / i);
      
       return 0;
    }
    stats sums(double Input[], int elements)
    {
       int i;
       static stats stat1;
      
       for(i = 0; i < elements; i++){
         if(Input[i] > 0){
             stat1.posCount += 1;
             stat1.posSum += Input[i];
         }
         else{
             stat1.negCount += 1;
             stat1.negSum -= Input[i];
         }
       }
       return stat1;
    }
    Last edited by Adak; 04-25-2013 at 04:28 PM.

  14. #14
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by Adak View Post
    I understand this to mean the sum() function can only have two input parameters - you have several pointers extra. Please explain.
    The description in post #1 says that there should be 2 input parameters and 4 output parameters, thus the approach using the pointers is correct IMHO.

    @craff25ff: What's your problem? The program in post #7 basically works (not sure about the output format; what about the possible division by zero?).

    Bye, Andreas

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by AndiPersti View Post
    The description in post #1 says that there should be 2 input parameters and 4 output parameters, thus the approach using the pointers is correct IMHO.

    @craff25ff: What's your problem? The program in post #7 basically works (not sure about the output format; what about the possible division by zero?).

    Bye, Andreas
    Explain to me if you would about this "input parameter" versus "output parameters", because I'm confused.

    I would classify everything in the parameter list as input, and only the return from the function, being output.

    Especially confusing is saying the array "pointer" (almost) is an input parameter, but several other pointers are all output parameters, even though they are receiving new input, inside the function??

    Is there a reference to this in K&R, someplace?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 08-21-2010, 12:50 PM
  2. I need some help with my C progam
    By ces4u in forum C Programming
    Replies: 2
    Last Post: 12-17-2005, 10:05 AM
  3. Help with multi function progam
    By WackoWolf in forum C Programming
    Replies: 22
    Last Post: 10-13-2005, 02:56 AM
  4. about decimal to hex progam
    By Abdi in forum C Programming
    Replies: 2
    Last Post: 06-01-2002, 07:08 PM
  5. Need help with text reversing progam..
    By ThunderCow in forum C++ Programming
    Replies: 3
    Last Post: 09-09-2001, 10:11 AM