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);

}

Recommended Answers

All 3 Replies

Even i'm prettty much new to C programming, but i'll try my best to help.

One of the errors is the illegal use of 'address of' operator. When you use the "&" in ur printf statements, it'll print the address of the variable and not it's value.

And if u r passing the address of a variable in a function, there should be a pointer variable which can hold the address value. the prototype and the function definition of fill_to_sent() do not match.

Perhaps I'm underestimating ur problem, but ur code seems a bit too complex for a pretty simple problem.

The following code takes input from the user and calculates the biggest element in the array and prints the subscript value as well.

#include<stdio.h>

int main()
{
    int a[20],i,n,big,subscript=0;
    printf("enter the no of elements (MAX IS 20!)\n");
    scanf("%d", &n);
    printf("Enter the elements\n");
    for(i=0;i<n;i++)
    scanf("%d",&a[i]);
    big=a[0];
    for(i=1;i<n;i++)
    {
           if(a[i] > big)
           {
                   big=a[i];
                   subscript=i;
           }
    }
    
    printf("Biggest element is %d\n", big);
    printf("Subscript is %d\n", subscript);
    getch();
    return 0;
}

Lemme know if i've missed something.
Cheers! :)

Thank you for your reply. I am notorious for making things too complicated. I will sit down with my code and simplify it.

I really don't understand the address-of operators or anything else in C programming at this point.
I do appreciate your help!!

Well, pointers are quite difficult to master initially; but they do get interesting once u get ur head around them.

anyway, best of luck with debugging ur code. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.