2017-10-19 6 views
-5

ヒストグラムであるプログラムを作成しようとしています。あなたはIndex:0,1,2,3,4,5,6,7,8,9とValuesを持っています:それらを挿入します。すべての値に対して、*を表示する必要があります。たとえば、値3 - > *** //値5 - > *****など。*を除いてすべてが完了しています。誰か私にアイデアやそれを行う方法の例を教えてもらえますか?おかげヒストグラムとC++で配列

Here there is the example of program.

#include <iostream> 
#include <stdlib.h> 
#include <iomanip> 

using namespace std; 

int main(int argc, char *argv[]) 
{ 
    // variabili per instogramma 
    int a=8; 
    int c=12; 
    int z=0; 

    // variabili per vettore 
    int v; 
    int numeri[10]; 
    int i=0; 

    do{ 
     cout<<"Inserisci i numeri:"; 
     cin>> v; 

     numeri[i]=v; 
     i+=1; 
    } while(i<10); 

    cout<<"\n"; 

    // Mostra Index - Elementi -  Instogramma 
    cout << setw(n) << "Index"; 
    cout << setw(a) << "Valori" << " "; 
    cout << setw(c) << "Instogramma\n"; 

    for(int z=0;z<10;z++) 
    { 
     cout << setw(n) << z; 
     cout << setw(a) << numeri[z] <<"\n"; 
    } 

    system("PAUSE");  
    return EXIT_SUCCESS; 
} 

And here there is what i got with my code

+4

"ヒストグラム"を意味しますか?そしてコードをインデントしてください。 –

+0

はい、ヒストグラムを意味します。 – folgore95

+1

1枚で1枚、2枚で2枚、3枚で3枚など問題はどこですか? –

答えて

1

文字列クラスは、それを繰り返すために、文字や回数を受け取るコンストラクタを持っています。その文字を含む文字列を作成します。だからあなたの場合

for (int z=0; z<10; z++) 
{ 
    cout<<setw(n)<<z; 

    cout<<setw(a)<<numeri[z]; 

    cout << string (numeri[z], '*') << "\n"; 

} 

私はあなたがそれの周りのスペースに対処する方法を見つけ出すもらおう。数が使用可能なスペースを超えた場合や、ゼロよりも小さい場合には、何をすべきかを決定する必要があります。

+0

'if(numeri [z] <0 || numeri [z]> SomeBigValue)// Do not print stars。 'をチェックしてください。そうでなければ、-42または+2,000,000,000になると面白い結果が得られるかもしれません。 –

+1

@CodeGorillaうん、有効なポイント - ありがとう – Steve

+0

これを行うと、位置cの文字列を*設定するとフィットしません。私はその行を<< setw(c)に挿入しましたが、 "ヒストグラムのテキスト"のようにはフィットしません。どうして? – folgore95

1

ループにを使用するのが最善の方法だと思います。 D幸運の男:

#include <iostream> 
#include <iomanip> 

using namespace std; 

int main() 
{ 
int disposable = 0; 
int a=8; 
int n=12; 
int c=10; 


cout<<"How many numbers do you want to enter?\n"; 
cin>>disposable; 
cout<<endl; 

int numbers[disposable]; 

for(int i = 0; i<disposable; i++) 
{ 
    cout<<"Enter a number: "; 
    cin>>numbers[i]; 
} 
cout<<setw(n)<<"Index"; 
cout<<setw(a)<<"Valori"<<" "; 
cout<<setw(c)<<"Instogramma\n"; 

for(int i = 0; i<disposable; i++) 
{ 
    cout<<setw(n)<<i; 
    cout<<setw(a)<<numbers[i]; 
    cout<<setw(c); 
    for(int j = 0; j<numbers[i]; j++) 
    { 
     cout<<"*"; 
    } 
    cout<<endl; 

} 

}

このコードは動作しますが、あなたは*さんの配向を固定する方法を理解する必要があります!ここで

+0

いいえ、このコードは機能しません。 – folgore95

+0

うまくいかない場合は、何か問題があります。異なるコンパイラを使用することもできます。 – Yoldrim

0

enter image description here

別のアイデアです。これは、ヒストグラムプログラムのベクトル配列バージョンです。 cinという行を1つだけ含む一連の数値をstringstreamの助けを借りて入力することができますが、唯一の違いは入力がvectorに格納されていることです。次に、入力に基づいてヒストグラムチャートを描画します。

<ENTER>キーを2回押すだけで、数字の入力が完了したことをプログラムに知らせることができます。

#include <iostream> 
#include <iterator> 
#include <vector> 
#include <algorithm> 
#include <sstream> 
using namespace std; 

vector<int> Vector; 
string line; 

void drawchart(int max); 


int main() { 

    cout<<"Chart drawing program (Histogram) \n"; 
    cout<<"Enter a series of numbers. \n"; 
    cout<<"Seperate with a space, press <ENTER> TWICE to end input \n"; 
    cout<<" (e.g 2 3 4 5 6) > "; 

    if(!getline(cin, line)) return 1; 
    istringstream iss(line); 

    copy(istream_iterator<int>(iss), istream_iterator<int>(), back_inserter(Vector)); 

    copy(Vector.begin(), Vector.end(), ostream_iterator<int>(cout, ", ")); 

    cout<<"\nDrawing chart.. \n\n"; 

    drawchart(Vector.size()); 


    cout<<"Press ANY key to close.\n\n";  
    cin.ignore();cin.get(); 

return 0; 
} 

// draws a chart or hjistogram 
void drawchart(int max){ 
    for(int i = 0; i < max ; i++){ 
     for(int j = 0; j < Vector[i]; j++) cout << "*"; 
     cout << endl; 
    } 
}