Thread: Beginner needs some help (started today)

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by tomtom1
    i simply dont know what i should do, therefore i will give up. thanks for the help anyways
    Well, that's your choice.

    The thing is, I don't understand why you say that you don't know what you should do. I mentioned a while loop and the checking of the return value of scanf to compare it with 1 in the loop condition:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int count = 0;
        int input;
        while (scanf("%d", &input) == 1)
        {
            ++count;
        }
        printf("%d numbers were read\n", count);
        return 0;
    }
    Notice that my example program has nothing about sum, or about the arithmetic mean ("midpoint"). It is a demonstration of one way of reading integers from standard input until EOF (or a read error) is detected. By getting this right first (after all, it is easy to count the numbers as you are entering them and then compare your count with the result that was printed), you can be more confident that if there's a bug later, it is most likely not because you were reading too few or too many numbers.

    Once you are sure about the count, you can add the part about computing the sum. Again, test: that way, if there's a bug later, it is most likely not because you were computing the sum wrongly. Once you have both count and sum, computing the arithmetic mean should not be a problem, and if it is a problem, chances are the bug lies with that computation, not elsewhere.
    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

  2. #17
    Registered User
    Join Date
    Oct 2014
    Posts
    13
    Im not trying to be mean or something dont get me wrong here, its just i have never programmed anything before and just decided to start with it 2 days ago. Im not sure what all those commands are doing or how you write the stuff etc. . I can just simply learn the things better when i see how they are done and not sitting 2 days in front of it doing try and error stuff.. Tbh im just trying things out at the moment cause i dont think at my current stage i can get anything done. And "not knowing what to do" ist just.. i dont know it.

    Nevertheless thank you for your help on counting the entries - thats what im actually came here for, to see how its properly done. I will try to sum the numbers now even tho i dont have much hopes.

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What material are you using to learn programming in C?
    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

  4. #19
    Registered User
    Join Date
    Oct 2014
    Posts
    13
    I dont have anything at the moment, currently searching for things that are very beginner friendly. As i mentioned I never programmed before, so its really difficult. I just jumped into the wild.

    So do i need another loop for summing the numbers up?
    Last edited by tomtom1; 10-18-2014 at 02:04 PM.

  5. #20
    Registered User
    Join Date
    Nov 2011
    Posts
    161
    Quote Originally Posted by tomtom1 View Post
    I dont have anything at the moment, currently searching for things that are very beginner friendly. As i mentioned I never programmed before, so its really difficult. I just jumped into the wild.

    So do i need another loop for summing the numbers up?
    What you need to do is study a book or take a lesson on C Programming.
    It is not a language you can 'jump into the wild' and produce anything of meaning without first learning it.
    There is plenty of free learning resources (even on this website) to learn the basics and beyond.
    If you don't build a foundation, you'll never have a building.

  6. #21
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    May be you could start with C Tutorial - Learn C - Cprogramming.com

  7. #22
    Registered User
    Join Date
    Oct 2014
    Posts
    13
    Yes i agree, but i want to finish this since i tried some stuff today ( for some hours). Dont want them to be purely wasted

    ps i ordered a really good book!
    Last edited by tomtom1; 10-18-2014 at 05:34 PM.

  8. #23
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I received a private message:
    Quote Originally Posted by tomtom1
    So we set the loop for counting the entries, do we need a loop in the loop to count the numbers, or another loop after it? Or do we need a loop at all? I think maybe a for loop now?
    If by "count the numbers" you actually mean "sum the numbers", then no, you can do that in the same loop as you read the numbers.
    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. #24
    Registered User
    Join Date
    Oct 2014
    Posts
    13
    ah, its working now!
    Last edited by tomtom1; 10-19-2014 at 09:46 AM. Reason: wron

  10. #25
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    To make your code readable, you need to format it properly. One of the important aspects of formatting is indentation:
    Code:
    #include <stdio.h>
      
    int main(void)
    {
        int count = 0;
        int input;
        int sum = 0;
        int i = 0;
        double avg;
     
     
        printf("Enter your numbers:\n");
        while (scanf("%d", &input) == 1){
            ++count;
            if (i < input){
                ++i;
                sum = sum + i;
            }
            avg = sum / count;
        }
        printf("%d Number(s) entered\n", count);
        printf("The sum is %d\n", sum);
        printf("The avg is %0.2f\n", avg);
        return 0;
    }
    Now, what is the purpose of the variable named i?
    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

  11. #26
    Registered User
    Join Date
    Oct 2014
    Posts
    13
    /edit Got it. Thank you very much for your help!
    Last edited by tomtom1; 10-19-2014 at 09:57 AM.

  12. #27
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Post your current code. It sounds like you have integer division when you want floating point division.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 07-30-2013, 01:28 AM
  2. Beginner C problems (Just started learning)
    By Millsie in forum C Programming
    Replies: 7
    Last Post: 05-19-2011, 03:37 PM
  3. I'm 30 today
    By Salem in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 05-16-2008, 06:36 AM
  4. What are you doing today?
    By Carlos in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 01-23-2002, 06:57 PM
  5. Today is...
    By Betazep in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 10-06-2001, 02:50 PM