Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void randArray(int randArray[], int arraySize);
- int countNumber(int inputArray[], int arraySize, int elementToCount);
- int main()
- {
- int array[ARRAYSIZE];
- int number;
- int occurrence;
- char choice;
- srand(time(NULL)); //Initierar slumpgeneratorn
- do {
- randArray(array, ARRAYSIZE);
- printf("\nEnter a number to search for: \n");
- scanf_s("%d", &number);
- occurrence = countNumber(array, ARRAYSIZE, number);
- printf("\nIt occurred %d times \n", occurrence);
- printf("Do you want to test again? (y/n) ");
- scanf_s(" %c", &choice);
- if (choice == 'n') {
- break;
- }
- if (choice != 'y' && choice != 'n') {
- printf("\nWrong input, do you want to test again? (y/n) ");
- scanf_s(" %c", &choice);
- }
- } while (choice == 'y');
- return 0;
- }
- void randArray(int randArray[], int arraySize)
- {
- for (int index = 0; index < arraySize; index++) {
- randArray[index] = rand() % arraySize + 1; //Fyller arrayen med randomtal (1-10)
- printf("%d \n", randArray[index]);
- }
- }
- int countNumber(int inputArray[], int arraySize, int numberToCount)
- {
- int occurrence = 0;
- for (int index = 0; index < arraySize; index++) //kollar igenom hela arrayen
- {
- if (inputArray[index] == numberToCount) //adderar 1 till occurrence varje gång inmatat tal stöts på i arrayen
- occurrence++;
- }
- return occurrence;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement