gautam0456
gautam0456

Reputation: 1

Why for loop is not taking element into an array?

I was trying to make a program that takes a series of numbers and prints the largest number. The logic is simple - take the series as an array and use the first step of insertion sort to check for the largest number. When I run the program, it goes till "Enter the number one by one". I enter a number and press enter, nothing happens for a second and the program terminates on itself. What did I miss in the first for loop?

#include <stdio.h>
void largest_number(int ar[], int size);
int main()
{
    int ar[100], index, size;
    printf("Enter total numbers = ");
    scanf("%d", &size);
    printf("Enter the numbers one by one\n");
    for (index = 0; index < size; index++)
        scanf("%d", ar[index]);
    largest_number(ar, size);
}

void largest_number(int ar[], int ar_size)
{
    int index, location = 0;
    for (index = 1; index < ar_size; index++)
        if (ar[index] > ar[location])
            location = index + 1;
    printf("The largest number is %d", ar[location]);
}

Upvotes: 0

Views: 91

Answers (2)

Farooq Baig
Farooq Baig

Reputation: 1

The answer is simple: You should use the address operator (&) to scan an element of an array. You've written ar[index] instead of &ar[index].

Upvotes: 0

William Pursell
William Pursell

Reputation: 212228

This response is more suited to https://codereview.stackexchange.com/, but there are a few issues in the sample code that appear with great enough frequency that they should be addressed. The primary mistake made in the sample code is the incorrect call to scanf, but there's more wrong than just failing to pass the address. You must always check the value returned by scanf. 98% of the code you'll find on the internet that uses scanf fails to do so. Don't assume your program will be run interactively; including prompts and things designed to be informative to an interactive user will render the program unusable in a non-interactive setting. A program which cannot be used non-interactively can never scale. (Sure, with toy programs like this you never expect it to scale, but it's good practice to get in the habit early.) Also, it's not that difficult to grow an array dynamically rather than using a fixed size array, so there's no need to require the input to contain an expected number of elements:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

struct int_array;
int largest_number(const int *ar, size_t size);
void push_int(struct int_array *a, int v);

struct int_array {
        int *ar;
        size_t cap;  /* Capacity of the buffer */
        size_t len;  /* Number of elements in the buffer */
};
int
main(void)
{
        struct int_array a = {0};
        int x;

        if( isatty(STDIN_FILENO) ){
                printf("Enter the numbers one by one\n");
                fflush(stdout); /* Probably not necessary */
        }
        while( scanf("%d", &x) == 1 ){ /* Always check return value */
                push_int(&a, x);
        }
        if( a.ar ){
                printf("The largest number: %d\n", largest_number(a.ar, a.len));
        }
}

void
push_int(struct int_array *a, int v)
{
        if( a->len >= a->cap ){
                a->ar = realloc(a->ar, sizeof *a->ar * (a->cap += 128));
                if( a->ar == NULL ){
                        perror("realloc");
                        exit(EXIT_FAILURE);
                }
        }
        a->ar[a->len++] = v;
}

int
largest_number(const int *ar, size_t ar_size)
{
        int max = *ar;
        for( const int *end = ar + ar_size; ar < end; ar += 1 ){
                if( *ar > max ){
                        max = *ar;
                }
        }
        return max;
}

If the user is entering data interactively, they can simply terminate the input by closing the input stream (eg, by typing ^d (on unix, I believe ^z on windows) or by entering some data that isn't an integer (eq, the string "quit").

Upvotes: 1

Related Questions