Write a main function to input 20 integers in the range of 1 to 6. Write a function to count the number of times the numbers 2 and 5 occur. The function should declare static variables count2 and count5. Check the data validity in the main function.
array = [] repetition = [0,0,0,0,0,0] for i in range 20 in = int(input()) % 6 + 1 array.append(in) repetition[in] += 1 This should be enough, though I dunno if you have seen arrays by the looks of the count2, count5. Also, this is pseudo code, please read the logic and translate it to whatever language you are learning
Here is the answer in C: /***************************************************************************************/ /* Program to input numbers from 1 to 6 and count the number of 2s and 5s */ /***************************************************************************************/ /* */ /* Input variable: */ /* numin = The number input by the user. */ /* */ /* Iteration variable: */ /* numcount = Counter for the while statement. */ /* */ /* Computed variables: */ /* cnt2 = Counter for the 2s. */ /* cnt5 = Counter for the 5s. */ /* */ /* Functions: */ /* funccount = Counter function for the 2s and 5s. */ /* funcoutput = Function to display the counter results. */ /***************************************************************************************/ # include <stdio.h> #include <iostream> /* Declare functions*/ void funccount(int, int * cnt2, int * cnt5); void funcoutput(int, int); /* Run the main function*/ int main() { /*Assign the variables*/ int cnt2 = 0, cnt5 = 0; int numin = 0; static int numcount = 1; /*Display the instruction line*/ printf("Input an Integer between 1 and 6 you will\n"); printf("do this 20 times!!!!\n"); /*Run the input and count loop*/ while(numcount<21) { printf("Input number:\n"); scanf("%d", &numin); /*Validate the input*/ if(numin>0 && numin<7) { /*Run the 2s and 5s counter*/ if(numin==2 || numin==5) { funccount(numin, &cnt2, &cnt5); } numcount = numcount + 1; } else printf("Invalid number try again!\n"); } printf("Integer input complete.\n"); /*Output the results through the function*/ funcoutput(cnt2, cnt5); system("PAUSE"); return 0; } /********************************/ /* Function to count 2s and 5s */ /********************************/ /* Static variables: */ /* count2 = 2s counter */ /* count5 = 5s counter */ /********************************/ void funccount(int numin, int * cnt2, int * cnt5) { /*Assign variables*/ static int count2 = 0; static int count5 = 0; /*Do the counting*/ if(numin==2) { count2 = count2 + 1; } else count5 = count5 + 1; *cnt2 = count2; *cnt5 = count5; return; } /**************************************************/ /* Function to display the number of 2s and 5s */ /*************************************************/ void funcoutput(int cnt2, int cnt5) { printf("There were %2d 2s entered.\n", cnt2); printf("There were %2d 5s entered.\n", cnt5); return; }
Join our real-time social learning platform and learn together with your friends!