Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.36 KB | None | 0 0
  1. void randArray(int randArray[], int arraySize);
  2. int countNumber(int inputArray[], int arraySize, int elementToCount);
  3.  
  4. int main()
  5. {  
  6.     int array[ARRAYSIZE];
  7.     int number;
  8.     int occurrence;
  9.     char choice;
  10.    
  11.     srand(time(NULL));    //Initierar slumpgeneratorn
  12.  
  13.     do {
  14.         randArray(array, ARRAYSIZE);
  15.  
  16.         printf("\nEnter a number to search for: \n");
  17.         scanf_s("%d", &number);
  18.  
  19.         occurrence = countNumber(array, ARRAYSIZE, number);
  20.  
  21.         printf("\nIt occurred %d times \n", occurrence);
  22.         printf("Do you want to test again? (y/n) ");
  23.         scanf_s(" %c", &choice);
  24.  
  25.         if (choice == 'n') {
  26.             break;
  27.         }
  28.         if (choice != 'y' && choice != 'n') {
  29.             printf("\nWrong input, do you want to test again? (y/n) ");
  30.             scanf_s(" %c", &choice);
  31.         }
  32.  
  33.     } while (choice == 'y');
  34.  
  35.     return 0;
  36. }
  37.  
  38. void randArray(int randArray[], int arraySize)  
  39. {
  40.     for (int index = 0; index < arraySize; index++) {
  41.        
  42.         randArray[index] = rand() % arraySize + 1;       //Fyller arrayen med randomtal (1-10)
  43.         printf("%d \n", randArray[index]);
  44.     }
  45. }
  46.  
  47. int countNumber(int inputArray[], int arraySize, int numberToCount)
  48. {
  49.     int occurrence = 0;
  50.  
  51.     for (int index = 0; index < arraySize; index++)      //kollar igenom hela arrayen
  52.     {
  53.         if (inputArray[index] == numberToCount)      //adderar 1 till occurrence varje gång inmatat tal stöts på i arrayen
  54.             occurrence++;
  55.     }
  56.    
  57.     return occurrence;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement