Home › Forums › C Programming › beginners needs help
This topic contains 4 replies, has 2 voices, and was last updated by GWILouisaxwzkla 9 years, 11 months ago.
-
AuthorPosts
-
December 29, 2009 at 8:32 am #2235
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 #3616
could try:
C123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354<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 #3617
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 #3618
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 #3619
Don’t know what type of sort you need to use but heres one with bubble sort:
C123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117<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 /><br />void bubbleSort ( int * array , int numberItems );<br /><br /><br />//main function ******************************<br /><br />const int MAX_NUMBERS = 20;<br /><br />int main ( )<br />{<br /><br />int input;<br />int numberOfInputItems;<br />int numberCount = 0;<br />int numbers [ MAX_NUMBERS ];<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 >> numbers [ i ] ;<br />if ( numbers [ i ] == 1 || numbers [ i ] == 4 || numbers [ i ] == 5 || numbers [ i ] == 12 ||<br />numbers [ i ] || numbers [ i ] == 3 )<br />numberCount ++;<br />i ++;<br />}<br /><br />cout < < endl;<br />cout < < "The count was " << numberCount << endl << endl;<br /><br />bubbleSort ( numbers , i );<br /><br />int j = 0;<br />while ( j < i )<br />{<br />cout < < numbers [ j ] << " ";<br />j ++;<br />}<br />}<br /><br />/******************************* FUNCTION DEFINITION ******************************<br /><br />Name : bubbleSort<br />Parameters :<br /><br />array a(n) int * ,<br />numberItems a(n) int<br /><br /><br />Returns: Void type<br />Comments:<br /><br /><br /><br />++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/<br />void bubbleSort ( int * array , int numberItems )<br />{<br /><br />int endSortedArray = numberItems - 1;<br />int lastSwapIndex;<br />int temp;<br />while ( endSortedArray > 0 ) //while not at the end of the unsorted array<br />{<br />lastSwapIndex = 0; //save index of the last item swapped<br />int i = 0; //start at the beggining of the unsorted array<br />while ( i < endSortedArray ) //while not in the sorted items<br />{<br />if ( array [ i ] > array [ i + 1 ] ) //if current item is smaller than next , bubble up<br />{<br />//swap array [ i ] and array [ i + 1 ]<br />temp = array [ i ];<br />array [ i ] = array [ i + 1 ];<br />array [ i + 1 ] = temp;<br />lastSwapIndex = i;<br />}<br />i ++;<br />}<br />endSortedArray = lastSwapIndex; //reset swap boundry<br />}<br /><br /><br /><br />return;<br />}<br /><br /><br /><br /> -
AuthorPosts
You must be logged in to reply to this topic.