Thread: Little second question from newbie(

  1. #1
    Registered User
    Join Date
    Aug 2022
    Posts
    4

    Question Little second question from newbie(

    Any advise? What is wrong? It gives meaningless result but as always it looks fine by me (

    Code:
    #include<stdio.h>
    
    
    int largest(int a[], int n)
    {
    int largestD;
    largestD = a[0];
    for(int i = 1; i < n; i++)
    if(a[i] > largestD) largestD = a[i];
    return largestD;
    }
    
    
    int average(int a[], int n)
        {
        int averageD = 0;
        for(int i = 0; i < n; i++)
        averageD += a[i];
        return averageD / n;
        }
    
    
    int positive(int a[], int n)
            {
            int h = 0;
            for(int i = 0; i < n; i++)
            if(a[i] > 0) h++;
            return h;
            }
    
    
    int main(void)
    {
    int n;
    int a[n];
    
    
    printf("Enter a lenght of array a: ");
    scanf("%d", &n);
    
    
    printf("Enter %d elements of array: ", n);
    for(int i = 0; i <= n - 1; i++)
    scanf("%d", &a[i]);
    
    
    for(int i = 0; i <= n - 1; i++)
    printf("%d ", a[i]);
    
    
    
    
    
    
    printf("\nLargest number is: %d\n", largest);
    
    
    printf("Average of aray is : %d\n", average);
    
    
    printf("Numbers of positive digits is: %d", positive);
    
    
    
    
    
    
    return 0;
    }
    Enter a lenght of array a: 5
    Enter 5 elements of array: 1
    2
    3
    4
    5
    1 2 3 4 5
    Largest number is: -444131880
    Average of aray is : -444131774
    Numbers of positive digits is: -444131693
    PS C:\Users\nirva\Desktop\c files\.vscode\Book_2\9_Function>
    Last edited by Nirvanko; 10-07-2022 at 06:18 PM.

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    You need to "call" the functions, not just mention their names.
    For example, instead of largest it should be largest(a, n)

    BTW, it is more idiomatic to write your loops like for (int i = 0; i < n; i++)
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Code:
    int n;
    int a[n];
    
    printf("Enter a lenght of array a: ");
    scanf("%d", &n);
    C doesn't revise the size of the array just because you assign a new value to n.

    Input n, then make your array.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Apr 2021
    Posts
    25
    To add to Salem's post, the "int a[n]" syntax creates what is known as a Variable-Length Array (VLA) which is not necessarily supported (you can read more about possible shortcomings here).

    Instead, you should preferably malloc the array like this (note that stdlib.h is also #included):
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    /* largest, average, positive definitions */
    
    int main(void)
    {
    int n;
    
    printf("Enter a lenght of array a: ");
    scanf("%d", &n);
    
    int *a = malloc(n * sizeof (*a)); // allocate n elements of the correct byte size
    
    /* With VLAs, the following would be a crash instead! */
    if (a == NULL) // A.K.A. the array can't fit in memory
    {
        fprintf(stderr, "Sorry, there's not enough memory for %d elements!\n", n);
        return 1; // no point in continuing execution
    }
    
    /* rest of main until last printf */
    
    free(a); // avoid memory leak
    return 0;
    }
    malloc will "make room" in memory (usually the heap part) for the array. "n" is the number of elements of a, and "sizeof (*a)" is the number of bytes in memory each of these elements needs.

    If there is not "enough" heap space left, it will simply return a NULL pointer. On the contrary, VLA allocation would not even try to make sure the array fits in the stack (another part of the memory).

    Remember, however, to always, ALWAYS free whatever you malloc by hand when you're done with it (never have a malloc without a matching free)!

  5. #5
    Registered User
    Join Date
    Aug 2022
    Posts
    4
    Thx to all for help! Appreciate it!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie Question
    By parallon in forum C Programming
    Replies: 9
    Last Post: 11-10-2018, 04:21 AM
  2. Hello, I am a newbie and so I have a newbie question;)
    By piratemonkey247 in forum C Programming
    Replies: 4
    Last Post: 12-20-2012, 10:59 AM
  3. Newbie with Very Newbie Question
    By Jedi_Mediator in forum C++ Programming
    Replies: 18
    Last Post: 07-01-2008, 08:00 AM
  4. C++ newbie / linux not so newbie question
    By goldmonkey in forum C++ Programming
    Replies: 7
    Last Post: 12-13-2003, 12:27 PM
  5. newbie Question
    By arnis in forum C++ Programming
    Replies: 2
    Last Post: 03-24-2002, 07:13 PM

Tags for this Thread