write the code in c Write a program that declares an integer array called values
ID: 3639910 • Letter: W
Question
write the code in c
Write a program that declares an integer array called values of size 55. Then get numbers from the user until the user enters a negative number and save the numbers in array values. Then in main do the following: Get the number of elements to print per-line from the user. This number should be from the interval [1,10] Use while input validation loop to insure the value entered is between 1 and 10(1 and 10 included) Call function printArray that prints the entire array to the screen Get a second integer (k) which will mean print the array starting from the k th clement For example, if the user entered 5 for k. then the program should print the array starting from the 5th element (which is the clement with index 4) Another example, if the user entered 2 for k, then the program should print the array starting from the 2 nd element (which is the clement with index 1) Call function printArray to print the array starting from the k th element Function printArray(): This function prints an arrray, L element per lineExplanation / Answer
#include void printArray(int arr[], int size, int L, int kth); int main () { int value[55]; int userInput; int elementsPerLine; int k; int counter = 0; //Get the user input //counter will be the number of elements they entered while(counter < 55) { printf("Please enter the %ith value: ", counter); scanf("%i", &userInput); if(userInput < 0) { break; } else { value[counter] = userInput; printf(" "); ++counter; } } userInput = 0; //reset the input while(userInput < 1 || userInput > 10) { printf(" Please enter the number of elements to print per line: "); scanf("%i", &userInput); } elementsPerLine = userInput; //If it makes it to here, their input was valid printArray(value, 55, elementsPerLine, k); //Print the array from the beginning userInput = 0; // reset the input while(userInput < 1 || userInput > 54) { printf(" Please enter the element to start printing from "); scanf("%i", &userInput); } k = userInput; //This is what element to start the printing from printArray(value, 55, elementsPerLine, k); return 0; } //The function to print the array to the screen //L is the number of elements per line void printArray(int arr[], int size, int L, int kth) { if(kth > 0) { --kth; } //So it prints from the correct element int count = 0; //while(count < 55) //{ for(; count < 55-kth; ++count) { //if(count < 55) // { if(count % L == 0) { printf(" "); } printf("%i ", arr[count+kth]); // } // else if(count == 55) break; } printf(" "); //} } Please rate LifeSaver. Thanks!Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.