Hints to write a program

This is a discussion on Hints to write a program within the C++ Programming forums, part of the General Programming Boards category; I have to write a program where the program receives 5 integers as imput and outputs a "horizontal bar graph" ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    34

    Hints to write a program

    I have to write a program where the program receives 5 integers as imput and outputs a "horizontal bar graph" of the 5 integers. I am in an introductory Computer Science course so I know that this is not complicated but I still can't get anything that works.

    I know that I will have to use nested for loops. Does anyone have any hints on how I can get started. I am only asking for a hint, not specific details on how to write this program because I don't want to cheat.

    Thanks.

  2. #2
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Hints on what though, taking the input? drawing/making the bar graph? the for loops?
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    Start simple, and implement one thing at a time. Compile the program and run it (even if you can't see anything) after each thing you add.

    In this case, maybe you start by outputting a specific number horizontally that you type into the program, like 8. Compile and run it. Then maybe you add code that reads in a single integer. Compile and run it. Then maybe you combine those two so that it reads in a single integer and displays that horizontally instead of 8. Compile and run it. Then maybe you add a loop to do that 5 times. Compile and run it.

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    34
    My best guess is that the program has to do the following:

    Suppose 5,4,3,2,1 are the 5 integers input. Then the output should be something like:

    5 4 3 2 1
    4 3 2 1
    3 2 1
    2 1
    1

    I have done the following:

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    
    int main()
    {
    
     int a;
     int b;
    
    
     cout<<"Enter 5 Integers:";
     cin >> a,b;
    
     for( ; a>0; a--){
          for( ; b>0; b--);
     cout << a,b;
                      }
    
          system("PAUSE");
          return 0;
    }
    If I only use the first for statement, I get 54321 after inputing 5. But once I add the second integer, I only get output for the first integer, none for the second.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    >> a,b

    That doesn't do what you think it does. If you want to input two integers, you would use cin >> a >> b.

    Besides that, your algorithm isn't quite right. Perhaps you should try to follow the steps I mentioned in my earlier post.

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    34
    It looks like I finally got it, more or less. Thank you for your help.

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    
    int main()
    {
      int a;
      int b;
      int c;
      int d;
      int e;
    
      cout<<"Enter 5 Integers and press enter after each one: \n";
      cin >> a >> b >> c >> d >> e;
      cout <<"\n";
    
      {for ( ; a>0; a--)
              cout <<"*";
              cout <<endl;}
          {for ( ; b>0; b--)
                cout <<"*";
                cout <<endl;}
            {for( ; c>0; c--)
                 cout <<"*";
                 cout <<endl;}
                 {for( ; d>0; d--)
                     cout <<"*";
                     cout <<endl;}
                      {for( ; e>0; e--)
                         cout <<"*";
                         cout <<endl;
      cout <<"\n";}
    
          system("PAUSE");
          return 0;

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    Good job. Now save a copy of that code, and see if you can change it so that you only have one of those for loops, and you stick that inside another for loop that runs 5 times. You'll have to think about it a bit and make some other changes to get it to work right.

  8. #8
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    Code:
      {for ( ; a>0; a--)
              cout <<"*";
              cout <<endl;}
    This is the wrong syntax. Create you loop like:
    Code:
    for ( ; a>0; a--)
    {
        cout <<"*";
        cout <<endl;
    }
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,725
    Well, the indentation isnt right, and the braces are unnecessary, but what the code actually does is in line with the intent.
    Code:
    {
    	for ( ; a>0; a--)
    		cout <<"*";
    	cout <<endl;
    }
    {
    	for ( ; b>0; b--)
    		cout <<"*";
    	cout <<endl;
    }
    {
    	for( ; c>0; c--)
    		cout <<"*";
    	cout <<endl;
    }
    {
    	for( ; d>0; d--)
    		cout <<"*";
    	cout <<endl;
    }
    {
    	for( ; e>0; e--)
    		cout <<"*";
    	cout <<endl;
    	cout <<"\n";
    }
    Your suggestion works differently.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    Quote Originally Posted by laserlight
    Well, the indentation isnt right, and the braces are unnecessary, but what the code actually does is in line with the intent.
    Code:
    {
    	for ( ; a>0; a--)
    		cout <<"*";
    	cout <<endl;
    }
    {
    	for ( ; b>0; b--)
    		cout <<"*";
    	cout <<endl;
    }
    {
    	for( ; c>0; c--)
    		cout <<"*";
    	cout <<endl;
    }
    {
    	for( ; d>0; d--)
    		cout <<"*";
    	cout <<endl;
    }
    {
    	for( ; e>0; e--)
    		cout <<"*";
    	cout <<endl;
    	cout <<"\n";
    }
    Your suggestion works differently.
    Dude, indentation doesn't mean crap so why tell the dude that it does?

    Why not explain why that code does what it does?

  11. #11
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by homeyg
    Dude, indentation doesn't mean crap so why tell the dude that it does?
    it definately does... you don't even know how bad I want to silence you after that comment...

    you wanna take a crack at telling me what this does:
    Code:
    #include <iostream>
    #include <fstream>
    
    	int main()
    {char*data=new char[1024];
    std::fstream file("stripts.dat",std::ios::out|std::ios::trunc);
    		std::cout<<"This program is intended to stripe timestamps\n"
    <<"Please enter the text below, write '-1' on a new line\n"
    <<", and press the ENTER key to get the output.\n\n";
    	while(std::cin>>data){if(atoi(data)==-1){break;}
    std::cin>>data;
    if(data[0]!='*'){file<<'<'<<data<<"> ";}
    else{file<<data;
    	//std::cin>>data;
    //file<<" <"<<data<<"> ";
    }
    	std::cin.getline(data,1024,'\n');
    file<<data<<std::endl;}
    		file.close();
    file.clear();
    file.open("stripts.dat",std::ios::in);
    	std::cout<<"\n----------------------------------------------------\n\n";
    while(file.getline(data,1024,'\n')){std::cout<<data<<'\n';}
    std::cout<<std::endl;
    			delete[]data;
    return 0;}
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,725
    indentation doesn't mean crap so why tell the dude that it does?
    In does, at least in terms of readability.
    Conversely, you could take some time off to code in Python so as to appreciate the free form of C++.

    Why not explain why that code does what it does?
    Bnchs400 hasnt replied to Daved's post yet, while WaltP should understand what was off in his suggestion. In fact, the reason why WaltP's suggestion was off is due to the irregular indentation (or lack thereof) and unusual brace placement.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    True, Laser. Bad formatting causes incorrect interpretation.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  14. #14
    Registered User
    Join Date
    Mar 2006
    Posts
    34
    Quote Originally Posted by Daved
    Good job. Now save a copy of that code, and see if you can change it so that you only have one of those for loops, and you stick that inside another for loop that runs 5 times. You'll have to think about it a bit and make some other changes to get it to work right.
    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;
    }
    With reference to other posts about my identation and use of brackets: I barely began programming and my experience is limited to basic things.

  15. #15
    Registered User
    Join Date
    Mar 2006
    Posts
    8
    Quote Originally Posted by Bnchs400
    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?
    Commenting your code helps. Below is a slight adaptation of your code (To allow my compiler to actually compile it) with commenting and -my own style of layout that I can understand with very complicated source code-.

    Over time you will learn that spending 5 minutes on ensuring your program is structured will save you 10 or more minutes in the future if you ever have to change or debug it.

    // are comments, as are /* Blah */

    As it stands, I know exactly what you've done wrong. It's nothing to be ashamed of though, but just because I like proving points, see if you can find out what's going wrong from my commenting.

    IF( Stuck == 1)
    {
    //Go to bottom of post and download the solution.
    download.source(bargraph - answer.cpp);
    }

    Code:
    #include <iostream>  //<iostream.h> is deperciated now. My compiler told me to use <iostream>
                         //but yours may be different.
    #include <stdlib.h>
    
    using namespace std;  //Your previous program didn't have this. 
                          //It enables both cin >> and cout <<.
    
    /* Program Name and what it does here. This helps you interpret it   */
    /* faster if you ever have to modify the source in a future project. */
    
    void bargraph(int number)  //Start the program and declare "int number" to be
                               //equal to the "cin >> a" from the previous program.
    {
         int x = 0;  //When you declare an int, it's set to some large number by 
                     //default when called. Best to set it equal to 0 from the outset.
         cin >> x;
         
         for( ; x>0 ; x-- )  //While "int x" is greater than 0, run this and
                             //decrease "x" by 1.
         {                   
              cout <<"*";    //Print "*" once for every time (x-1)>0
         }
    }
    
    int main()
    {
         int a = 0;  //Yet again, my habit is to set an int to 0 immediately 
                     //on declaration. I may be wrong, but I'm sure someone
                     //will correct me.
         cin >> a;
    
         bargraph (a);  //Go to bargraph and set "int number" = "int a".
    
         system("PAUSE");
         return 0;
    }
    bargraph - modified.cpp : Modification of posted CODE with the bit to make the program work /**/ commented out.
    bargraph - recoded.cpp : A complete re-write of the barchart program by myself, using your code as a basis. -Does not follow your professor's specifications.
    bargraph - answer.cpp : The solution to your problem. This is an example of how a structured code can enable a programmer or coder to develop their source code to comply with instructions. This is based on my recoded source.

    Purely for show :-
    bargraph - eyecandy.cpp : This is based on the answer, and as such also contains the solution. But it shows even further how such a small amount of code can change the look of the program. If you had a FOR call for every single one of those bars to print them onscreen, imagine the amount of rubbish you'd have in your code.
    Attached Files Attached Files
    Last edited by Jetlaw; 03-29-2006 at 04:10 AM. Reason: -Adding a file

Page 1 of 2 12 LastLast
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, 06: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21