I wrote this program and I need to know if I didn't make a mistake

Let arr be an array of 20 integers. Write a program that first fill the array with up to 20 input values and then finds and displays both the subscript of thee largest item in arr and the value of the largest item.

/* 181415.c */

#include <stdio.h>

void main() {
    int arr[20];    /* define an array of 20 integers */
    int i;  
    int max;
    int max_index;  

    // Input 20 integers
    printf("Input 20 integers: \n");
    for (i = 0; i < 20; i++) {
        printf("Integer %d: ", i);
        scanf("%d", &arr[i]);
    }
    // Find the largest integer and its index
    max = arr[0]; 
    max_index = 0;
    for (i = 1; i < 20; i++) {
        if (arr[i] > max) {
            max = arr[i];
            max_index = i;
        }
    }
    // Print out the max integer and its index
    printf("The largest integer is %d\n", max);
    printf("Its position is at index %d\n", max_index);
    getchar();
}

Recommended Answers

All 2 Replies

Yes this will perform the request.

>I wrote this program and I need to know if I didn't make a mistake
Would you care to know if you did? void main() is in opposition to the standard. It should return an integer. Therefore the proper code would be int main(void) in your case.

It is time that you start using the proper tags when you post your code. Click here for an example.

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.