hi everyone im new to c++ and sorry for my english.i want to make a basic program which count digits in number.after program starts we enter 5 number.after then program counts digits in number and shows digits used how many times in number.
dos screen must seem lik this:
enter integers between 1-5: 1
12
123
1234
12345//we enter these numbers after program start
1:5 times
2:4 times
3:3 times
4:2 times
5:1 times
enter integers between 1-5:
_
i would be delighted if you help me.
thanks in advance:)
/* traverse a map/set is O(n log n) (or at least O(n) )
for (map<int, int>::iterator iter = imap.begin(); iter != imap.end(); iter++)
if (iter->first == result)
iter->second++;*/
imap[result]++; // binary search O(log n)
i want to make a basic program which count digits in number
#include <iostream>
usingnamespace std;
int main()
{
int num[5]={0,0,0,0,0};
int y=0;
char x;
do { //Loops while x is equal or higher then 1 and equal or lower then 5
cin >> x;
/* x == '1'?num[0]++:num[0]==num[0];//if x = 1 num[0]adds 1
x == '2'?num[1]++:num[1]==num[1];//if x = 2 num[1]adds 1
x == '3'?num[2]++:num[2]==num[2];//if x = 3 num[2]adds 1
x == '4'?num[3]++:num[3]==num[3];//if x = 4 num[3]adds 1
x == '5'?num[4]++:num[4]==num[4];//if x = 5 num[4]adds 1
can obviously be simplified as ne555 says in the folowing post as:*/
num[ x-'1' ]++;
}
while ( x>='1',x<='5');
cout << "calculating ...\n";//Loop ended, displays the amount of each num occured
cout << "1 occured " << num[0] << " times.\n";
cout << "2 occured " << num[1] << " times.\n";
cout << "3 occured " << num[2] << " times.\n";
cout << "4 occured " << num[3] << " times.\n";
cout << "5 occured " << num[4] << " times.\n";
cin.get();
cin.get();//pause
return 0;
}
btw if someone knows a alternative for the 0,0,0,0,0 in this line: int num[5]={0,0,0,0,0}; pls tell me :)
seems like I get a error if i didn't type it
kinda want to assign every variable in the array to 0 :)