Thread: Hints to write a program

  1. #16
    Registered User
    Join Date
    Mar 2006
    Posts
    34
    [QUOTE=Bnchs400]Actually, today my professor gave us a new assignment to do the same thing, this time using a single for loop in a function.

    When the program is run, I have to enter two numbers and only the second number gets the "*" output. Does anyone have a suggestion on how to fix this?

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    
    void bargraph(int number)
    {
    int x;
    cin >>x;
    {for( ; x>0; x--)
         cout <<"*";
         }
         }
    
    
    int main()
    {
    int a;
    cin>> a;
    
      bargraph (a);
    
    
    
          system("PAUSE");
          return 0;
    }
    The last assignment for this program is to modify this code. The new program should use a function, for loop, and an array of maximum size 20. The user defines how many numbers are input and the output should be each number that was input. For example:

    If the user defines 3 numbers and enters an input of 3, 4, 5 the output should be:

    ***
    ****
    *****

    I have made progress but still am not receiving the right output.

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    
    void bargraph(int number)
    {
    for( int i=0; i <number; i++)
     cout <<"*";
     cout <<endl;
    }
    
    
    int main()
    {
     int bar[20];
     int number;
    
     cout << "How many Numbers to Print Stars For? ";
     cin >> number;
    
     for(int i = 0; i < number; i++)
      {
      cout << "Enter a Number: ";
      cin >> bar[i];
      }
    
     bargraph(number);
    
          system("PAUSE");
          return 0;
    }
    Any suggestions would be greatly appreciated. Thanks.

  2. #17
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Sorry if I am horribly wrong but I thought you declared a function first then did main then write what the funstion does.

  3. #18
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> I thought you declared a function first then did main then write what the funstion does.

    It doesn't matter. Defining the function first is fine.

  4. #19
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    By the way, <iostream.h> and <stdlib.h> are deprecated. Use <iostream> and the std namespace.

    Also, using for() without curly braces is bad.

    Code:
    for(int i = 0; i == WACKY_JOHNSON; ++i)
    abc();
    def();
    What if you wanted to add ghi() to the loop?

    Code:
    for(int i = 0; i == WACKY_JOHNSON; ++i)
    abc();
    ghi();
    def();
    wouldn't work as expected. Always use curly braces.



    Getting to the point: you only run bargraph() once. This will only print the bar graph for the last number entered. Also, all the values are stored in bar[]; but you do bargraph() on number. You need two loops: one for number input, and one to display a bar for each number.

    If you have a problem, try running through each step of the program in your mind to see if it's doing what you want.

  5. #20
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    Code:
    for(int i = 0; i < number; i++)
    {
        cout << "Enter a Number: ";
        cin >> bar[i];
    }
    you can exceed the 20 limit -> program will crash.

    Put bargraph() in two nested for loops?:

    Code:
    int i;
    
    for(i = 0; i < numbers; i++)
    {
        for(int ind = 0; ind < i; i++)
        {
            bargraph(bar[i]);
        }
    }
    something like that?
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  6. #21
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> By the way, <iostream.h> and <stdlib.h> are deprecated.
    <iostream.h> is not deprecated, it is completely non-standard. <stdlib.h> is deprecated and so it will work on all standards compliant compilers.

  7. #22
    Registered User
    Join Date
    Mar 2006
    Posts
    34
    So the program is now doing what it is supposed to do, except that it outputs the stars after entering a number. I can't figure out how to get it to print out the stars after all of the numbers have been entered.

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    
     void bargraph(int number)
    {
    for( int i=0; i <number; i++)
     cout <<"*";
     cout <<endl;
    }
    
    
    int main()
    {
     int bar[20];
     int number;
    
     cout << "How many Numbers to Print Stars For? ";
     cin >> number;
    
     for(int i = 0; i < number; i++)
      {
      cout << "Enter a Number: ";
      cin >> bar[i];
      bargraph(bar[i]);
      }
    
          system("PAUSE");
          return 0;
    }

  8. #23
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    If you want to gather all the numbers up first, and then go through all the numbers and do the bar graph second, then it would make sense to separate those two tasks instead of putting them next to each other in the same loop.

  9. #24
    Registered User
    Join Date
    Mar 2006
    Posts
    34
    Quote Originally Posted by Daved
    If you want to gather all the numbers up first, and then go through all the numbers and do the bar graph second, then it would make sense to separate those two tasks instead of putting them next to each other in the same loop.
    Yes, but when I put the function call outside of the for loop, I get an error message because bar[i] is not declared outside the for loop. At the same time, I need to use some form of bar[] because the numbers are stored in this array.
    Last edited by Bnchs400; 04-04-2006 at 06:31 PM.

  10. #25
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Think about it without the code. How would you accomplish the task if you had to ask somebody for 20 numbers, and you had to write down the numbers he said, then you had to draw a bar for each number in the graph? If you can write that out in English how you would accomplish that task, in as much detail as you are capable of giving, then that would greatly help you solve the problem.

  11. #26
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    I get an error message because bar[i] is not declared outside the for loop.
    Why? Because i is not declared. Just run the same loop again, this time not asking for each number, but printing the bar graph for each number.

    You seriously need to brush up on your program design skills.

  12. #27
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    I get an error message because bar[i] is not declared outside the for loop.
    Why? Because i is not declared. Just run the same loop again, this time not asking for each number, but printing the bar graph for each number.

    You seriously need to brush up on your algorithm design skills.

  13. #28
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    I get an error message because bar[i] is not declared outside the for loop.
    Why? Because i is not declared. Just run the same loop again, this time not asking for each number, but printing the bar graph for each number.

    You seriously need to brush up on your algorithm design skills.

  14. #29
    Registered User
    Join Date
    Mar 2006
    Posts
    34
    I followed your suggestions and added the following for loop to the end of the program:

    Code:
     
    for(int i = 0; i < number; i++)
      {
      bargraph(bar[i]);
      }
    It now works how it is supposed to. Thanks for the help again.

    You seriously need to brush up on your algorithm design skills.
    The class I'm taking is just a "sample" of the programming I will have to do in the next course. As a beginner, I do not have much experience. Over time, I will learn the best/correct way to plan an algorithm and write a program. After all, practice makes perfect.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  2. Replies: 6
    Last Post: 01-01-2007, 07:36 AM
  3. Replies: 1
    Last Post: 10-13-2004, 12:15 PM
  4. Can I write & read file in same program?
    By chad03 in forum C Programming
    Replies: 2
    Last Post: 08-04-2003, 08:39 AM
  5. Challenge to write a program
    By Twisted.alice in forum A Brief History of Cprogramming.com
    Replies: 40
    Last Post: 05-15-2003, 12:00 PM