0

Given an array of integers, calculate the fractions of its elements that are positive, negative, and are zeros. Print the decimal value of each fraction on a new line.

code written by me:

#include <bits/stdc++.h>
#include <iostream>

using namespace std;

int main()
{
    int n;
    int a[n];
    float sum1=0,sum2=0,sum3=0;
    cin>>n;

    for(int i=0;i<n;i++)
    {
        cin>>a[i];
    }
    for(int i=0;i<n;i++)
    {
        if(a[i]<0)
        {
           sum1++ ;       
        }
        else if(a[i]>0)
        {
            sum2++ ;
        }
        else
        {
            sum3++ ;
        }
    }

     cout<<(sum2/n)<<endl;
     cout<<(sum1/n)<<endl;
     cout<<(sum3/n)<<endl;
}
James Z
  • 12,209
  • 10
  • 24
  • 44
NightOx
  • 9
  • 4
  • you initialized an array on the stack and then attempted to access a dynamically defined size. move the insertion of n via cin to the top and declare the array dynamically via new or better yet use the std::vector container. – Adam May 31 '20 at 07:57
  • This is wrong: `int n; int a[n];` In the latter case `n` must be a compile-time constant. – SurvivalMachine May 31 '20 at 07:57
  • Thank you sir for clarification. I now know where I did mistake. – NightOx May 31 '20 at 08:08
  • You should not use `#include ` or `using namespace std;` in your code. You can read more about it at [why should I not include stdc++](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) and [why should I not use using std](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Kerek May 31 '20 at 11:27

1 Answers1

2

This program is accepting only 5 inputs while i am giving n=8, kindly help me out to find the error

The int n; (in the function main()) declares a variable with automatic storage class and it has garbage value (unless you initialize it int n = 100;). So, it's undefined what value variable n will hold and since, you are using it to declare an array of int with size n, the array may have random size. So, running such a program may give you some undefined behavior that may vary from machine to machine or may be you get different behavior if you run the same program in the same machine multiple times.

The compiler is free to do anything from crashing to summoning demons through your nasal passages.

The right way to do this is:

#define MAX_ARRAY_SIZE    100

int n = 0;
int arr[MAX_ARRAY_SIZE];

or

int n = 0;
std::cin >> n;
int arr[n];

or using std::vector

#include <iostream>
#include <vector>
#include <algorithm>

int main(void)
{
    int n;
    std::vector<int> arr;
    std::cin >> n;

    for(int i = 0; i < n; i++)
    {
        int e;
        std::cin >> e;
        arr.push_back(e);
    }
    int sum1 = std::count_if(std::begin(arr), std::end(arr), [] (int num) { return num < 0; });
    int sum2 = std::count_if(std::begin(arr), std::end(arr), [] (int num) { return num > 0; });
    int sum3 = n - (sum1 + sum2);

    std::cout << (float(sum2) / n) << std::endl;
    std::cout << (float(sum1) / n) << std::endl;
    std::cout << (float(sum3) / n) << std::endl;

    return 0;
}
abhiarora
  • 9,743
  • 5
  • 32
  • 57
  • Great. Check my latest edit. Don't forget to upvote and accept my answer. – abhiarora May 31 '20 at 08:12
  • when n=6 it was working correctly but when n=8 it was not .. can you shade some light into it.. – NightOx May 31 '20 at 08:16
  • 1
    You mean your old program? It could be because your array be of size other than `8` but when you write to your array. You must be writing to some other variables on the stack. This is called "buffer overflow". – abhiarora May 31 '20 at 08:20
  • 1
    So, it's undefined behavior. You may get different results if you run the same program in different machine. – abhiarora May 31 '20 at 08:21