5 Replies - 387 Views - Last Post: 19 June 2013 - 01:07 PM Rate Topic: -----

#1 dodies  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 19-June 13

array for frequency

Posted 19 June 2013 - 10:40 AM

hye..im new with c++
can someone help me...
my program did not calculate the frequency of the user input..
it show "0" for each input
this is my code
please ignore the question language :D

#include<iostream>
#include <stdio.h>
#include<string>
using namespace std;

int main()
{
	int num[7];

	
string  personality;



cout<<"                    question 1"<<endl;

cout<<"A)Apakah ciri-ciri yang mesti ada apada pasanganmu?"<<endl<<endl;

cout<<"1)tinggi, kacak, dan bergaya"<<endl;
cout<<"2)Agama ,akhlak baik dan menghormati ibu bapa"<<endl;
cout<<"3)Penyayang, romantis dan baik hati"<<endl<<endl<<endl;


while (true) {
cout << "Jawapan anda.\n ";
        cin >> num[7];
        if (cin.fail())
        {
            cout << "Not a number " << endl;
            cout << "Please enter a number from 1 to 3, inclusive.\n";
            cin.clear();
			cin >> num[7];
        }
        if (num[7] >= 1 && num[7] <= 3) break;
        cout << "Please enter a number from 1 to 3, inclusive.\n";
    }


cout<<"                    question 2"<<endl;

cout<<"B)Kamu lebih selesa jika pasanganmu selalu?"<<endl<<endl;

cout<<"1)Ada tika suka dan duka"<<endl;
cout<<"2)Mengambil berat dan sentiasa menjagamu"<<endl;
cout<<"3)Tidak terlalu mengongkong kamu "<<endl<<endl<<endl;


while (true) {
cout << "Jawapan anda.\n ";
        cin >> num[1];
        if (cin.fail())
        {
            cout << "Not a number " << endl;
            cout << "Please enter a number from 1 to 3, inclusive.\n";
            cin.clear();
            cin >> num[1];
        }
        if (num[1] >= 1 && num[1] <= 3) break;
        cout << "Please enter a number from 1 to 3, inclusive.\n";
    }


cout<<"                    question 3"<<endl;

cout<<" C)Berapa lamakah tempoh perhubungan kalian setakat ini?"<<endl<<endl;

cout<<"1)Satu tahun hingga dua tahun"<<endl;
cout<<"2)Tiga tahun hingga empat tahun"<<endl;
cout<<"3)Lima tahun dan keatas : "<<endl<<endl<<endl;

while (true) {
cout << "Jawapan anda.\n ";
        cin >> num[2];
        if (cin.fail())
        {
            cout << "Not a number " << endl;
            cout << "Please enter a number from 1 to 3, inclusive.\n";
            cin.clear();
            cin >> num[2];
        }
        if (num[2] >= 1 && num[2] <= 3) break;
        cout << "Please enter a number from 1 to 3, inclusive.\n";
    }


cout<<"                    question 4"<<endl;

cout<<"D)Bagaimana cara komunikasinya dengan kamu?"<<endl<<endl;

cout<<"1)Sopan dan agak pemalu"<<endl;
cout<<"2)Sempoi dan suka mengusik"<<endl;
cout<<"3)Tegas "<<endl<<endl<<endl;

while (true) {
cout << "Jawapan anda.\n ";
        cin >> num[3];
        if (cin.fail())
        {
            cout << " " << endl;
            cout << "Please enter a number from 1 to 3, inclusive.\n";
            cin.clear();
            cin >> num[3];
        }
        if (num[3] >= 1 && num[3] <= 3) break;
        cout << "Please enter a number from 1 to 3, inclusive.\n";
    }


cout<<"                    question 5"<<endl;

cout<<"E)Pernahkah si dia berkorban demi kamu?"<<endl<<endl;

cout<<"1)Kadang-kadang itupun sebab terpaksa"<<endl;
cout<<"2)Selalu dan sentiasa ada untuk kamu"<<endl;
cout<<"3)Pernah tetapi akan diungkit jika berlaku perselisihan paham "<<endl<<endl<<endl;


while (true) {
cout << "Jawapan anda.\n ";
        cin >> num[4];
        if (cin.fail())
        {
            cout << "Not a number " << endl;
            cout << "Please enter a number from 1 to 3, inclusive.\n";
            cin.clear();
            cin >> num[4];
        }
        if (num[4] >= 1 && num[4] <= 3) break;
        cout << "Please enter a number from 1 to 3, inclusive.\n";
    }

cout<<"                    question 6"<<endl;

cout<<" F)Adakah pasanganmu mudah bertoleransi?"<<endl<<endl;

cout<<"1)Sukar! kalau A maka A lah jawapnye"<<endl;
cout<<"2)Boleh sekiranya perkara itu dirasakan baik dan sesuai"<<endl;
cout<<"3)Ya! Mudah dan tidak pernah menggunakan kuasa veto"<<endl<<endl<<endl;

while (true) {
cout << "Jawapan anda.\n ";
        cin >> num[5];
        if (cin.fail())
        {
            cout << "Not a number " << endl;
            cout << "Please enter a number from 1 to 3, inclusive.\n";
            cin.clear();
            cin >> num[5];
        }
        if (num[5] >= 1 && num[5] <= 3) break;
        cout << "Please enter a number from 1 to 3, inclusive.\n";
    }


cout<<"                    question 7"<<endl;

cout<<" G)Adakah pasanganmu sedia berkongsi masalah dengan kamu?"<<endl<<endl;

cout<<"1)Mungkin tidak! dia seorang yang perahsia."<<endl;
cout<<"2)Ya! Dia akan sentiasa berkongsi cerita dengan kamu"<<endl;
cout<<"3)Mungkin ya, jika dirasakan perlu dan tidak jika ia agak privasi "<<endl<<endl<<endl;

while (true) {
cout << "Jawapan anda.\n ";
        cin >> num[6];
        if (cin.fail())
        {
            cout << "Not a number " << endl;
            cout << "Please enter a number from 1 to 3, inclusive.\n";
            cin.clear();
            cin >> num[6];
        }
        if (num[6] >= 1 && num[6] <= 3) break;
        cout << "Please enter a number from 1 to 3, inclusive.\n"<<endl;

    }



cout<<"kekerapan jawapan anda"<<endl<<endl;

int sorted[7];
int N = sizeof(num)/sizeof(num[0]);
  int last_change=0;
  
for (int i=1; i<4; ++i){ 
    int n;
    for (n = last_change; n<N && num[n]==i; ++n) {
    }
    cout << i << " berulang " << (n-last_change) << " kali" <<endl;
	last_change = n;


}

system("pause");
return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: array for frequency

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4133
  • View blog
  • Posts: 12,868
  • Joined: 25-December 09

Re: array for frequency

Posted 19 June 2013 - 10:53 AM

Quote

please ignore the question language

For us to ignore the language you'll need to provide more information.

What is the purpose of your program?

What input do you enter into your program?

What output do you expect you program to produce with the given input?

Jim
Was This Post Helpful? 0
  • +
  • -

#3 dodies  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 19-June 13

Re: array for frequency

Posted 19 June 2013 - 11:30 AM

sorry for that

this is personality test program
they are 7 question..the user will choose answer 1,2, or 3 for each question

that means the program will get 7 answer.

then the program calculate the most frequent answer
for example
question 1 =user choose 1
question 2 =user choose 3
question 3 =user choose 2
question 4 =user choose 1
question 5 =user choose 1
question 6 =user choose 2
question 7 =user choose 3

frequent answer is 1
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is online

  • member icon


Reputation: 4133
  • View blog
  • Posts: 12,868
  • Joined: 25-December 09

Re: array for frequency

Posted 19 June 2013 - 11:52 AM

One thing I noticed with a quick glance is that you're accessing your array out of bounds.

	int num[7];

...
while (true) {
cout << "Jawapan anda.\n ";
        cin >> num[7];


Arrays in C/C++ start at zero and stop at size - 1.

Also you're not handling the entry of a non-number correctly. You have cleared the error flags but you still need to remove the offending characters from the buffer before you try to get a new number. You may want to investigate the ignore() member function.

Why all the code duplication? You may want to consider a loop of some kind instead of all the duplication.


Jim
Was This Post Helpful? 0
  • +
  • -

#5 dodies  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 19-June 13

Re: array for frequency

Posted 19 June 2013 - 12:23 PM

View Postjimblumberg, on 19 June 2013 - 11:52 AM, said:

One thing I noticed with a quick glance is that you're accessing your array out of bounds.

	int num[7];

...
while (true) {
cout << "Jawapan anda.\n ";
        cin >> num[7];


Arrays in C/C++ start at zero and stop at size - 1.

Also you're not handling the entry of a non-number correctly. You have cleared the error flags but you still need to remove the offending characters from the buffer before you try to get a new number. You may want to investigate the ignore() member function.

Why all the code duplication? You may want to consider a loop of some kind instead of all the duplication.


Jim


thanks for that info..
i just started learning c++ 3 weeks ago..
i just have basic...will search for that info..thank you
Was This Post Helpful? 0
  • +
  • -

#6 dodies  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 19-June 13

Re: array for frequency

Posted 19 June 2013 - 01:07 PM

View Postdodies, on 19 June 2013 - 12:23 PM, said:

View Postjimblumberg, on 19 June 2013 - 11:52 AM, said:

One thing I noticed with a quick glance is that you're accessing your array out of bounds.

	int num[7];

...
while (true) {
cout << "Jawapan anda.\n ";
        cin >> num[7];


Arrays in C/C++ start at zero and stop at size - 1.

Also you're not handling the entry of a non-number correctly. You have cleared the error flags but you still need to remove the offending characters from the buffer before you try to get a new number. You may want to investigate the ignore() member function.

Why all the code duplication? You may want to consider a loop of some kind instead of all the duplication.


Jim


thanks for that info..
i just started learning c++ 3 weeks ago..
i just have basic...will search for that info..thank you


yeahh...now my program working fine ...thanks to you jim!!
cheers!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1