Home Forums C Programming beginners needs help

Viewing 4 reply threads
  • Author
    Posts
    • #2235
      JerrodBillingto
      Participant

      i have an assignment…i cant do it…
      how can i count the number i input
      example
      example

      i 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

    • #3616
      GWILouisaxwzkla
      Participant

      could try:



      /****************************************************************
      * 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;

      //main function ******************************

      int main ( )
      {

      int input;
      int numberOfInputItems;
      int numberCount = 0;

      cout << "Enter number of items to input: ";
      cin >> numberOfInputItems;

      int i = 0;
      while ( i < numberOfInputItems )
      {
      cout << "enter a number: ";
      cin >> input;
      if ( input == 1 || input == 4 || input == 5 || input == 12 ||
      input == 6 || input == 3 )
      numberCount ++;
      i ++;
      }

      cout << endl;
      cout << "The count was " << numberCount << endl;
      }

    • #3617
      JerrodBillingto
      Participant

      thank you so very very much!…

      one more question…
      how can in arrange the numbers in ascending order?…happy new year!

    • #3618
      JerrodBillingto
      Participant

      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!

    • #3619
      GWILouisaxwzkla
      Participant

      Don’t know what type of sort you need to use but heres one with bubble sort:

      
      
      /****************************************************************
      * 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 : bubbleSort
      Parameters :
      
      array a(n) int * ,
      numberItems a(n) int
      
      
      Returns: Void type
      Comments:
      
      
      
      ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
      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 swapped
      int i = 0; //start at the beggining of the unsorted array
      while ( 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;
      }
      
      
      
      
Viewing 4 reply threads
  • The forum ‘C Programming’ is closed to new topics and replies.