0
    #include<stdio.h>
    int main()
    { 
    int get()
    {
      int n;
      int sum=0;
      int  i;
      int a[i];
      printf("enter the value of n");
      scanf("%d",&n);
      if(n==5)
      {
         printf("ok");
      }
      else
      {
        printf("not ok dont enter next value re run the program");
      }
      printf("enter the values of array");
     for(int i=1;i<=n;i++)
     {
      scanf("%d",&a[i]);
     }
     printf("the sum of array is ");
     for(int i=1;i<=n;i++)
     {
      sum=sum+a[i];
      printf("%d\n",sum);
     }
     }
     get(); 
     return 0;  
     }

but when i write below code it doesnot give the output as i am expecting

    #include<stdio.h>
    int main()
    { 
    int get()
    {
      int n;
      int sum=0;
      int  i;
      int a[i];
      printf("enter the value of n");
      scanf("%d",&n);
      if(n==5)
      {
         printf("ok");
      }
      else
      {
        printf("not ok dont enter next value re run the program");
      }
      printf("enter the values of array");
     for(int i=1;i<=n;i++)
     {
      scanf("%d",&a[i]);
     }
     printf("enter the values of array");
     for(int i=1;i<=n;i++)
     {
      printf("%d",a[i]);
     }
     printf("the sum of array is ");
     for(int i=1;i<=n;i++)
     {
      sum=sum+a[i];
      printf("%d\n",sum);
     }
     }
     get(); 
     return 0;  
     }

it does not give the expected output please help

Problem

You are given an array A consisting of N integers.

Task

Print the sum of the elements in the array.

Note: Some of the integers may be quite large.

Input Format

The first line contains a single integer N denoting the size of the array.
The next line contains space-separated integers denoting the elements of the array.

Output format

Print a single value representing the sum of the elements in the array.

Constraints

1<=N<=10

0<=a[i]<=10^10

Sample Input

5
1000000001 1000000002 1000000003 1000000004 1000000005

Sample Output

5000000015

         #include <cmath>
         #include <cstdio>
         #include <vector>
         #include <iostream>
         #include <algorithm>
         using namespace std;
         typedef long long int list;


        int main() {
        /* Enter your code here. Read input from STDIN. Print output to 
        STDOUT */   
        list n,sum=0;
        cin>>n;
        while(n--)
        {
        list a;
        cin>>a;
        sum+=a;
        }
        cout<<sum;
        return 0;
        }

this program gave the correct solution.its quite complex to understand but it works according to the expectations.

        #include <stdio.h>
     int main() 
     {  
     int i, n, value;
     long long sum = 0;
     scanf("%d", &n);
     for (i = 0; i < n; i++)
     {
     scanf("%d",&value);
     sum =sum + value;
     }
     printf("%lld", sum);
     return 0;
     }

this c program is accepted by machine which is much simpler form except how variable value is acting as an array without [] these symbols

  • 3
    `int i;int a[i];` When you declare your array, the value of `i` is indeterminate, because `i` wasn't initialized. Use the max. value for N given in the problem. Also note that an array `int a[N]` has valid indices from `0` to `N - 1`. All your loops are off by one. – M Oehm Mar 27 '22 at 17:18
  • 3
    Both programs presented exhibit nested functions. This is not a feature defined by standard C. Start by moving function `get()` outside and ahead of function `main()`. Or, alternatively, by just collapsing them into a single function. – John Bollinger Mar 27 '22 at 17:18
  • Do not ask for help in the title or in the text. *Every question on the site* is asked to get help, so there is no need to explicitly ask for it. – tucuxi Mar 27 '22 at 17:19
  • 1
    See [Why is this VLA (variable-length array) definition unreliable?](https://stackoverflow.com/questions/70792452/why-is-this-vla-variable-length-array-definition-unreliable) – Jonathan Leffler Mar 27 '22 at 17:20
  • if any one cant understand the problem please make a comment i will try to make you understand the problem more clearly. – Syed Obaidullah Hussaini Mar 27 '22 at 17:25
  • 1
    If `n` may only be 5, why ask for user input? If the input is invalid and the user must restart the program (a somewhat user-hostile approach to error handling!), you should probably exit the program there and then rather than carry on regardless. What is the array even for? You could simply accumulate the sum as the values are entered and discarded. Not much about this code makes much sense t.b.h. Don't use nested functions, don't use VLAs and certainly do not instantiate a VLA using an undefined value for its size! – Clifford Mar 27 '22 at 17:26
  • 1
    The only one that does not understand the problem perhaps is yourself. You have the problem you are asking about, then all the other problems this code exhibits. Maximise the warning level, use a debugger (a much more productive debug technique that posting questions on SO). – Clifford Mar 27 '22 at 17:28
  • Note that the value range 0 to 10^10 requires an unsigned integer type with at least 34 bits (i.e. not an int). The sum of 10 such numbers [requires 37 bits](https://www.google.com/search?q=log%28%2810%5E10%29*10%29%2Flog%282%29). – Clifford Mar 27 '22 at 17:32
  • "_it does not give the expected output_" : What output _do_ you get? Great that you posted the code and the expected output, but the actual output will allow others to see your problem without necessarily running the code and applying the input themselves. – Clifford Mar 27 '22 at 17:40
  • Start with the first and most serious issue - the first variable `i` is unitialised and used nowhere other then in the declaration of `n`. `n` may end up too small and writing to it will corrupt adjacent variables, or it may be so big it exceeds the processes stack allocation. The variable `i` is then unused throughout (and shadowed by the loop control variables). – Clifford Mar 27 '22 at 17:46
  • What is the first block of code? You don't mention it in the text. Does it differ from the second block? If you want others to read your code, you should endeavour to format and indent it conventionally. – Clifford Mar 27 '22 at 17:48
  • Apart from the output formatting `int n[10]` supports the `1<=N<=10` constraint and sums correctly (though I have not tested it extensively). However to meet `0<=a[i]<=10^10`, you will need `unsigned long long n[10] ;` and corresponding changes to the `scanf()` input. You should endeavour to write code that considers all parts of the assignment - as it is it is a bit "cargo-cult"ish. – Clifford Mar 27 '22 at 17:55
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Community Mar 30 '22 at 23:27

1 Answers1

0

There are multiple problems in your code:

  • it is very difficult to read because the presentation is bad. You should use indentation consistently, 4 characters is considered preferable.

  • you cannot define local functions in Standard C. Define get() before the main() function or write the code directly inside the main() function.

  • defining the array int a[i]; with i uninitialized has undefined behavior. You could define the array as int a[n]; after you read the number of elements n, but there is actually no need for an array: you can compute the sum as you read the values.

  • you should use type long long for sum to help avoid overflow if the sum exceeds INT_MAX.

  • The more complex program is written in C++, which is a different language, originally inspired by the C language, but quite different today. The code uses type long long via a very confusing list typedef. The while(n--) is risky: the loop will keep reading values if the user enters a negative value and there is no error handling for invalid input.

Here is a modified version:

#include <stdio.h>

int main() {  
    int i, n, value;
    long long sum = 0;

    printf("enter the value of n: ");
    if (scanf("%d", &n) != 1) {
        printf("invalid input, re run the program\n");
        return 1;
    }
    printf("enter the values of array: ");
    for (i = 0; i < n; i++) {
        if (scanf("%d", &value) != 1) {
            printf("invalid input, breaking from the loop\n");
            break;
        }
        sum += value;
    }
    printf("the sum of array is %lld\n", sum);
    return 0;  
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189