Home › Forums › C Programming › beginners needs help
- This topic has 4 replies, 2 voices, and was last updated 14 years, 11 months ago by GWILouisaxwzkla.
Viewing 4 reply threads
- AuthorPosts
- December 29, 2009 at 8:32 am #2235JerrodBillingtoParticipant
i have an assignment…i cant do it…
how can i count the number i input
example
examplei input
1
4
5
12
6
1
3
4
5
how can i count if howmany 1, 4 , 5, 12, 6, 3 are there..
really need help
- December 29, 2009 at 5:08 pm #3616GWILouisaxwzklaParticipant
could try:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354<br /><br />/****************************************************************<br />* File Name : c:programstempCG.cpp<br />* Date : December,29,2009<br />* Comments : new project<br />* Compiler/Assembler : Visual C++ 6.0<br />* Modifications :<br />*<br />*<br />*<br />*<br />*<br />* Program Shell Generated At: 11:37:28 a.m.<br />=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/<br /><br /><br />#include < iostream ><br />//#include < string.h ><br />//#include < conio.h ><br />//#include < math.h ><br />//#include < iomanip ><br />//#include < ctype.h ><br /><br />using namespace std;<br /><br />//main function ******************************<br /><br />int main ( )<br />{<br /><br />int input;<br />int numberOfInputItems;<br />int numberCount = 0;<br /><br />cout << "Enter number of items to input: ";<br />cin >> numberOfInputItems;<br /><br />int i = 0;<br />while ( i < numberOfInputItems )<br />{<br />cout << "enter a number: ";<br />cin >> input;<br />if ( input == 1 || input == 4 || input == 5 || input == 12 ||<br />input == 6 || input == 3 )<br />numberCount ++;<br />i ++;<br />}<br /><br />cout << endl;<br />cout << "The count was " << numberCount << endl;<br />}<br /><br /> - December 31, 2009 at 8:29 am #3617JerrodBillingtoParticipant
thank you so very very much!…
one more question…
how can in arrange the numbers in ascending order?…happy new year! - December 31, 2009 at 8:38 am #3618JerrodBillingtoParticipant
what if the program requires me to input a maximum of 20 numbers then count the number of similar numbers?…and arrange the numbers that the user input in ascending order…THANK YOU!
- January 2, 2010 at 9:40 pm #3619GWILouisaxwzklaParticipant
Don’t know what type of sort you need to use but heres one with bubble sort:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117/***************************************************************** File Name : c:programstempCG.cpp* Date : December,29,2009* Comments : new project* Compiler/Assembler : Visual C++ 6.0* Modifications :****** Program Shell Generated At: 11:37:28 a.m.=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/#include < iostream >//#include < string.h >//#include < conio.h >//#include < math.h >//#include < iomanip >//#include < ctype.h >using namespace std;void bubbleSort ( int * array , int numberItems );//main function ******************************const int MAX_NUMBERS = 20;int main ( ){int input;int numberOfInputItems;int numberCount = 0;int numbers [ MAX_NUMBERS ];cout << "Enter number of items to input: ";cin >> numberOfInputItems;int i = 0;while ( i < numberOfInputItems ){cout << "enter a number: ";cin >> numbers [ i ] ;if ( numbers [ i ] == 1 || numbers [ i ] == 4 || numbers [ i ] == 5 || numbers [ i ] == 12 ||numbers [ i ] || numbers [ i ] == 3 )numberCount ++;i ++;}cout << endl;cout << "The count was " << numberCount << endl << endl;bubbleSort ( numbers , i );int j = 0;while ( j < i ){cout << numbers [ j ] << " ";j ++;}}/******************************* FUNCTION DEFINITION ******************************Name : bubbleSortParameters :array a(n) int * ,numberItems a(n) intReturns: Void typeComments:++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/void bubbleSort ( int * array , int numberItems ){int endSortedArray = numberItems - 1;int lastSwapIndex;int temp;while ( endSortedArray > 0 ) //while not at the end of the unsorted array{lastSwapIndex = 0; //save index of the last item swappedint i = 0; //start at the beggining of the unsorted arraywhile ( i < endSortedArray ) //while not in the sorted items{if ( array [ i ] > array [ i + 1 ] ) //if current item is smaller than next , bubble up{//swap array [ i ] and array [ i + 1 ]temp = array [ i ];array [ i ] = array [ i + 1 ];array [ i + 1 ] = temp;lastSwapIndex = i;}i ++;}endSortedArray = lastSwapIndex; //reset swap boundry}return;}
- AuthorPosts
Viewing 4 reply threads
- The forum ‘C Programming’ is closed to new topics and replies.