带有数组的C ++直方图

时间:2017-10-19 10:21:08

标签: c++ arrays

我试图创建一个直方图程序。您将拥有索引:0,1,2,3,4,5,6,7,8,9和值:您插入它们。对于需要显示*的每个值。例如,值3 - > *** //值5 - > *****等我除了*之外我已经完成了所有工作。有人可以给我一个想法或一个如何做的例子吗?感谢

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

3 个答案:

答案 0 :(得分:1)

字符串类有一个构造函数,它接受一个字符并多次重复它。它将构造一个包含许多字符的字符串。

所以在你的情况下:

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

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

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

}

我会告诉你如何处理它周围的间距。请注意,如果数字超出可用空间或小于零,您可能还需要决定该怎么做。

答案 1 :(得分:1)

我认为最好的方法是使用进行循环。

#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;

}

}

这段代码有效,你必须弄明白如何修复*的对齐:D祝你好运!

答案 2 :(得分:1)

enter image description here

这是另一个想法。它是直方图程序的矢量数组版本。它允许您在cin的帮助下输入一系列只有stringstream行的数字,但唯一的区别是它将输入存储在vector中。然后根据输入绘制直方图。

只需按两次<ENTER>键即可让程序知道您输入了数字。

#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;
    }
}