Hello everyone. Like so many others, I am new to programming. I am taking a course on Programming in C, and have hit a number of speed bumps.
The assignment I am currently stumped on is:
Let arr be an array of 20 integers. Write a program that first fills the array with up to 20 input values and then finds and displays both the subscript of the largest item in arr and the value of the largest item.
Here is what I have for code so far. Please help me figure out what the heck I am doing wrong. This is all like ancient Greek to me!!!
/*Finds the largest integer value in an array and displays the value and the subscript*/
#include <stdio.h>
#define MAX_INT 20
#define SENT -99
int fill_to_sent(SENT, int arr[], int *arr_sizep);
int get_max(int *const arr[], int n);
int get_sub(int *const arr[], int n);
int
main(void)
{
int arr[MAX_INT], /*data list */
in_use, /*number of elements filled */
cur_large, /*largest value so far */
large_sub; /*subscript of largest value */
/*Fills array with up to 20 integers */
fill_to_sent(SENT, arr[], &in_use);
/*Finds largest integer in array */
get_max(const arr[], in_use);
/*Finds subscript value for largest integer*/
get_sub(const arr[], in_use);
printf("The largest integer entered is %d and the subscript value is %d\n", &cur_large, &large_sub);
return(0);
}
/*Gets integers to fill arr until the sentinel value is encountered.
*Pre: SENT and MAX_INT are defined and MAX_INT is the declared size of arr.*/
int
fill_to_sent(SENT, /*input - end of data value in input list */
int arr[], /*output - array of data */
int &in_use);/*output - number of array elements filled */
{
int data,
i,
status;
printf("Please enter up to 20 integers.\n");
printf("When you are finished entering integers,\n");
printf("enter %d to show you are done.\n", &SENT);
i = 0;
status = scanf("%d", &data);
while (status == 1 && data != SENT && i < MAX_INT){
arr[i] = data;
++i;
status = scanf("%d", &data);
}
/*Issues error message for incorrect data */
if (status != 1) {
printf("\n*** Error in data format ***\n");
printf("*** Using first %d integers entered ***\n", i);
} else if (data != SENT) {
printf("\n*** Error: too many integers were entered ***\n");
printf("*** Using first %d integers entered ***\n", i);
}
*arr_sizep = i;
}
/* Returns the largest integer in the used array elements */
int
get_max(int *const arr[], /*input - list of intergers */
int arr_sizep) /*input - number of elements to examine */
{
int i,
cur_large; /*largest value so far */
/*Initial array element is largest so far */
cur_large = arr[0];
/*Compare each remaining list element to the largest so far and save the larger */
for (i = 1; i < arr_sizep; ++i)
if (arr[i] > cur_large)
cur_large = arr[i];
return (cur_large);
}
/*Returns the subscript value of the largest integer */
int
get_sub(int *const arr[],
int arr_sizep)
{
int i,
large_sub;
large_sub = 0;
for (i = 1; i < arr_sizep; ++i)
if (arr[i] > arr[large_sub])
large_sub = i;
return (large_sub);
}