In: Computer Science
Concepts to Practice • Pointers • Simulated “pass by reference” via pointers • Strings • Relationship of pointers to arrays
Description For the prelab assignment, you need to implement a program that issues prompts and takes in data from the user inside various functions. There are special rules in this assignment to make sure you pass variables back and forth between your functions and the main() function: • You may not call scanf() from your main() function. • You may not use global variables. • The variables you print out at the end must all be declared in your main() function. • All of your functions must have a void return type, except as described below. The main() function in your program should: 1. Print a message welcoming the user to Prelab 8. 2. Call a function to get an integer and a floating point variable from the user. 3. Call a function to get a string from the user. 4. Call a function to get an integer array from the user. 5. Print out all of the values that the user entered. Functions You Must Write You may write any functions you wish to implement this program, but there must be a function to get an integer and a float, a function to get a string, and a function to get an array. To get the array, you must write a function with the following prototype: int GetIntegerArray(int * outputIntArray, int maxsize); The GetIntegerArray() function should return the actual number of array elements entered. Its parameters are the location in memory where it should begin filling in the array elements (outputIntArray), and the maximum number of elements (maxsize) that you can enter (so you won’t run off the end of the array). Sample Output jimr@JimRArea51:~/CS1050/FS2020/labs/lab8$ compile prelab8.c jimr@JimRArea51:~/CS1050/FS2020/labs/lab8$ ./a.out *********************** * Welcome to Prelab 8 * *********************** Please enter an integer followed by a space followed by a float and then hit enter: 123 27.391 Please enter a string without spaces and then hit enter: MizzouBeatTheDefendingNationalChampions! How many integers would you like to input? 4 Enter integer #1: 18 Enter integer #2: 23 Enter integer #3: 475 Enter integer #4: 32768 *********************** You entered 123,27.39,MizzouBeatTheDefendingNationalChampions! Array elements: array[0]=18 array[1]=23 array[2]=475 array[3]=32768
Please find the requested program below. Also including the screenshot of sample output and screenshot of code to understand the indentation.
Please provide your feedback
Thanks and Happy learning!
#include <stdio.h>
void getIntAndFloatFromUser(int &intInput, float &floatInput)
{
printf("Please enter an integer followed by a space followed by a float and then hit enter : ");
scanf("%d%f", &intInput, &floatInput);
}
void getStringFromUser(char* stringInputArray)
{
printf("Please enter a string without spaces and then hit enter : ");
scanf("%s", stringInputArray);
}
int GetIntegerArray(int * outputIntArray, int maxsize)
{
int inputCount = 0;
while (inputCount < 1 || inputCount > 100)
{
printf("How many integers would you like to input ? ");
scanf("%d", &inputCount);
if (inputCount < 1 || inputCount > 100)
{
printf("Input count is bigger than the maximun size(100) allowed.\n");
}
}
for (int i = 0; i < inputCount; i++)
{
printf("Enter integer #%d: ", i+1);
scanf("%d", &outputIntArray[i]);
}
return inputCount;
}
int main()
{
int intInput = 0;
int totalElementsInIntArray = 0;
float floatInput = 0;
char stringInputArray[100] = { '\0' };
int outputIntArray[100];
printf("*********************** * Welcome to Prelab 8 * ***********************\n");
getIntAndFloatFromUser(intInput, floatInput);
getStringFromUser(stringInputArray);
totalElementsInIntArray = GetIntegerArray(outputIntArray, 100);
printf("*********************** You entered ***********************\n");
printf("%d, %.2f, %s\n", intInput, floatInput, stringInputArray);
printf("Array elements : ");
for (int i = 0; i < totalElementsInIntArray; i++)
{
printf("array[%d] = %d ", i, outputIntArray[i]);
}
return 0;
}