Code question..

This is a discussion on Code question.. within the C++ Programming forums, part of the General Programming Boards category; Hi i have made this simple code: Code: #include <iostream> #include <stdlib.h> #include <iomanip> using namespace std; int main(int argc, ...

  1. #1
    Registered User snapshooter's Avatar
    Join Date
    Sep 2004
    Posts
    37

    Code question..

    Hi i have made this simple code:

    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <iomanip>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
    
    int MyArray[10];
    
    
    for(int i=0;i<10;i++){
        cout<<setw(7)<<"MyArray["<<i<<"] = "<<i<<endl;
        cout<<setw(16)<<"Value for Element "<<i<<"  = "<<1+rand()%10<<endl;
     
    
    }    
      system("PAUSE");	
      return 0;
    }
    my problems are:

    1. i want to make an Histogram, for the random value..
    for example if i have a value 10, i want to print 10 asterisks..

    2. how to set the output to this:

    MyArray[] Value Histogram
    0----------> 4 ----> ** **
    1----------> 2 ----> **


    Thanks in advance..

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    use a nested loop, two loops deep. The outer loop will initialize a variable to control how many times through the inner loop you will go. the inner loop will output a single character (asterix) each time through the loop. You can put a space between each group of two character if you wish. When output for inner loop completed go to the next line before starting next time through inner loop.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >1. i want to make an Histogram, for the random value..
    >for example if i have a value 10, i want to print 10 asterisks..

    Another idea is to make a string filled with *'s. Then use the substr() function to display a certain number of *'s based on the value in MyArray[i].

  4. #4
    Registered User
    Join Date
    Oct 2004
    Posts
    120
    Quote Originally Posted by swoopy
    >1. i want to make an Histogram, for the random value..
    >for example if i have a value 10, i want to print 10 asterisks..

    Another idea is to make a string filled with *'s. Then use the substr() function to display a certain number of *'s based on the value in MyArray[i].
    This method is limiting. What if I only have 20 '*' in my string and the value is 21?

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    the number of * in the string could be determined based on user input.


    int size;
    cout << "enter # of * to use as max per line" << endl;
    cin >> size;
    string str;
    for(int i = 0; i < size; ++i)
    str += "*";

    or better yet:

    string str(size, '*');

    should work.

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    The easy way (a modification of what I think elad is saying):

    Code:
    #include <string>
    #include <iostream>
    using namespace std;
    ...
    int MyArray[10];
    ...
    for( int j = 0; j < 10; ++j )
        cout << string(MyArray[j],'*') << endl;
    This simply uses the string constructor to automatically generate a string of '*' characters of length MyArray[j] for each element of the array.
    Last edited by hk_mp5kpdw; 12-02-2004 at 12:25 PM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    for( int j = 0; j < 10; ++j )
    cout << string(MyArray[j],'*') << endl;

    well done.

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > cout << string(MyArray[j],'*') << endl;
    Awesome, I like the looks of that!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hi all i have a question about a code
    By iweapons in forum C++ Programming
    Replies: 5
    Last Post: 05-23-2007, 11:05 AM
  2. I need help to compile this code...
    By wise_ron in forum C Programming
    Replies: 17
    Last Post: 05-07-2006, 12:22 PM
  3. My First If Code, Question? :P
    By dimirpaw in forum C++ Programming
    Replies: 3
    Last Post: 11-29-2005, 07:49 PM
  4. End of Code Loop Question
    By JuanSverige in forum C++ Programming
    Replies: 1
    Last Post: 04-08-2003, 10:35 AM
  5. question about my code
    By killerasp in forum C++ Programming
    Replies: 7
    Last Post: 02-18-2002, 07:05 PM

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