Thread: newbie needs help on small program

  1. #1
    Registered User
    Join Date
    Oct 2012
    Location
    Forest Hill, Maryland, United States
    Posts
    39

    newbie needs help on small program

    I am teaching myself and need to create a program:

    that will read in five positive integers (one at a time) and print out the largest and smallest number of the 5 numbers. The program should read the numbers 1 at a time.
    Also I need this program such that it takes an unlimited amount of positive integers.

    Here is a starting attempt which may be all wrong:

    Code:
    #include<stdio.h>
    
    int main() {
    
    char number[5];
    for( ; ; ; )  {
    printf("%5d\n", number++);
    }
    
    return 0;
    }
    I would really appreciate any input or solution!

    Dodgetech

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Where do you want the program to get the 5 numbers from (the user or a file perhaps)?

    I see the char array, so I'm wondering, do you want 5 DIGITS, or 5 NUMBERS, at a time.

    Any infinite loop in a program, is (almost) 100% wrong. Somehow, someway, every loop, needs a way to let the user or the program, exit from it.

    So, more info, please.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    that will read in five positive integers...
    Also I need this program such that it takes an unlimited amount of positive integers.
    These are conflicting requirements. Do you need to take in five integers, or unlimited integers?

  4. #4
    Registered User
    Join Date
    Oct 2012
    Location
    Forest Hill, Maryland, United States
    Posts
    39
    Quote Originally Posted by Adak View Post
    Where do you want the program to get the 5 numbers from (the user or a file perhaps)?

    I see the char array, so I'm wondering, do you want 5 DIGITS, or 5 NUMBERS, at a time.

    Any infinite loop in a program, is (almost) 100% wrong. Somehow, someway, every loop, needs a way to let the user or the program, exit from it.

    So, more info, please.

    Well basically I want 2 programs where one will take infinite integers and the other taking 5 single integers.

    I guess I also wrote the code wrong. I want a user to be able to input 5 single digits, not a 5 digit number. I hope this clarified.

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    I think that you should take a look at this:

    Introduction to C - Cprogramming.com

    There are lots of C tutorials to get you on your way
    Fact - Beethoven wrote his first symphony in C

  6. #6
    Registered User
    Join Date
    Oct 2012
    Location
    Forest Hill, Maryland, United States
    Posts
    39
    Quote Originally Posted by Click_here View Post
    I think that you should take a look at this:

    Introduction to C - Cprogramming.com

    There are lots of C tutorials to get you on your way

    Yes I have done some tutorials, but I was hoping someone could get me started. I understand code after I see a solution, but have hard time coming up with the code when a problem is asked of me.

    Guess I am on my own here.....

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Here's how you should approach it.

    Code:
     - Put the computer aside for the moment, and grab a pen and paper
     - Solve the problem by hand, keeping note of each step you must take
     - Do this a few times until you have a list of steps to follow
     - Open your source file
    while(!complete)
    {
     - Write a few lines of code to do just one thing
     - Compile and run this code to ensure it is doing what you expect
    }
    My recommendation: Start with the "5 input" variation first, this will be easier.

    Step 1: Write the basic framework of the program (which you have in your first post, but don't include the "for()" loop).

    Step 2: Write a loop that simply receives user input and places it into your array, and loop it 5 times. Compile and verify there are no errors or warnings.

    Step 3: Write a loop that prints each element of that array, so you can see the results. Even if you don't require this output as part of your program, it will help you verify that your code is working as expected. (Coding is like constructing a building, in that it often requires the assembly of temporary "scaffolding" to help achieve the main goal). Compile and verify there are no errors or warnings.

    Step 4: Write the code that searches for the largest number, and prints it out. Compile and verify there are no errors or warnings.

    Step 5: Write the code that searches for the smallest number, and prints it out. Compile and verify there are no errors or warnings.

    Take it step by step. If you get stuck, feel free to ask additional questions - ensure that those questions are specific if you want good, specific answers.

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by dodgetech View Post
    Yes I have done some tutorials, but I was hoping someone could get me started. I understand code after I see a solution, but have hard time coming up with the code when a problem is asked of me.
    The best way to learn to write code is to write code. Having someone write it for you would only teach you better how to get others to write it for you.

    If you want to learn, then learn. Go through tutorials that are appropriate and actually write code. If you're not willing to put in the time, then neither are we.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    Registered User
    Join Date
    Oct 2012
    Location
    Forest Hill, Maryland, United States
    Posts
    39
    Quote Originally Posted by Matticus View Post
    Here's how you should approach it.

    Code:
     - Put the computer aside for the moment, and grab a pen and paper
     - Solve the problem by hand, keeping note of each step you must take
     - Do this a few times until you have a list of steps to follow
     - Open your source file
    while(!complete)
    {
     - Write a few lines of code to do just one thing
     - Compile and run this code to ensure it is doing what you expect
    }
    My recommendation: Start with the "5 input" variation first, this will be easier.

    Step 1: Write the basic framework of the program (which you have in your first post, but don't include the "for()" loop).

    Step 2: Write a loop that simply receives user input and places it into your array, and loop it 5 times. Compile and verify there are no errors or warnings.

    Step 3: Write a loop that prints each element of that array, so you can see the results. Even if you don't require this output as part of your program, it will help you verify that your code is working as expected. (Coding is like constructing a building, in that it often requires the assembly of temporary "scaffolding" to help achieve the main goal). Compile and verify there are no errors or warnings.

    Step 4: Write the code that searches for the largest number, and prints it out. Compile and verify there are no errors or warnings.

    Step 5: Write the code that searches for the smallest number, and prints it out. Compile and verify there are no errors or warnings.

    Take it step by step. If you get stuck, feel free to ask additional questions - ensure that those questions are specific if you want good, specific answers.

    Thanks Matticus, this was a big help to get me started. I guess I came across in the wrong way. I didn't expect someone to just straight write the whole code for me. I just needed some suggestions.
    My biggest issue I have run across was the code for searching for the lowest and highest integer.

    Thank you all.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by dodgetech
    My biggest issue I have run across was the code for searching for the lowest and highest integer.
    What idea do you have for searching for the lowest integer?
    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. #11
    Registered User
    Join Date
    Oct 2012
    Location
    Forest Hill, Maryland, United States
    Posts
    39
    Quote Originally Posted by laserlight View Post
    What idea do you have for searching for the lowest integer?
    I have not gotten to this part yet, but I am guessing I will use an if-else structure. I suppose I can assign each of the 5 integers an abstract letter eg. a, b, c, d e, then use if a< b type code. Or maybe I also need a switch/case structure.

    does that sound like a correct way? or I may be way off....


    Dodgetech

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I would expect you to use a loop to loop over the elements of the array. Creating separate variables for each of the 5 integers would be a wrong approach. (Well, it can work here, but is completely infeasible when you can potentially have a billion integers.)
    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

  13. #13
    Registered User
    Join Date
    Oct 2012
    Location
    Forest Hill, Maryland, United States
    Posts
    39
    Quote Originally Posted by laserlight View Post
    I would expect you to use a loop to loop over the elements of the array. Creating separate variables for each of the 5 integers would be a wrong approach. (Well, it can work here, but is completely infeasible when you can potentially have a billion integers.)
    Alright here is what I came up with even though I know there is a shorter way.

    Code:
    #include <stdio.h>
    
     
    int main()
    {
        int a=0, b=0, c=0, d=0, e=0, min=0, max=0;
         
        // Ask values from the user and put values to min and max
        printf("Enter value of A: ");
        scanf("%d", &a);
        min = a;
        max = a;
        if(a<min)
        min = a;
        if(a>max)
        max = a;
         
        printf("Enter value of B: ");
        scanf("%d",&b);
        if(b<min)
        min = b;
        if(b>max)
        max = b;
         
        printf("Enter value of C: ");
        scanf("%d", &c);
        if(c<min)
        min = c;
        if(c>max)
        max = c;
         
        printf("Enter value of D: ");
        scanf("%d", &d);
        if(d<min)
        min = d;
        if(d>max)
        max = d;
         
        printf("Enter value of E: ");
        scanf("%d", &e);
        if(e<min)
        min = e;
        if(e>max)
        max = e;
         
         
        printf("Largest is: %d\n", max);
        printf("Smallest is: %d\n", min);
            
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please Help with small program
    By cjohnman in forum C Programming
    Replies: 11
    Last Post: 04-14-2008, 09:59 AM
  2. Help with a small program!!
    By tnexpress in forum C Programming
    Replies: 8
    Last Post: 11-02-2005, 01:42 PM
  3. A little Help with a small program
    By whtpirate in forum C Programming
    Replies: 7
    Last Post: 06-05-2003, 06:15 PM
  4. A small problem with a small program
    By Wetling in forum C Programming
    Replies: 7
    Last Post: 03-25-2002, 09:45 PM
  5. Help with a small program
    By nmbr1sun99 in forum C Programming
    Replies: 11
    Last Post: 02-12-2002, 12:30 AM