-1
int function(int A[], int n)
{

    int i = 0;
    int sum = 0;
    int amount = 0;

    while(i<n) {
        if(A[i] > 0) {
            sum=sum+A[i];
            amount++;
        }
        else {
            i++;
        }
    }
    while(!(i<n)) {
        if(ile>0){
            return sum/amount;
        } else {
            return 0;
        }
    }

}

I am generating random array of numbers between 0-10 , Im trying to use this with this algorithm, but all the time im getting result 6422260. Can someone tell me how should I approach this?

   int n;
    cin >> n;
    int arr[n];
    srand(time(NULL));

    for (int i = 0; i < 10; i++)
    {
     arr[i] = rand() % 10;
    }
    function(arr, n);

Dave
  • 1
  • 1
  • 9
  • 38
  • Why function do not have return statement out of while loop – Abrar Malekji Jan 23 '21 at 09:17
  • What is the fathomable point of that `while (!(i < n))` loop, which has two hard returns, and *none* after the outer loop, thereby leading to undefined behavior if `(!(i < n))` is false form inception? – WhozCraig Jan 23 '21 at 09:18
  • @AbrarMalekji , I think i am writing it wrong :/ I've added in my post how algorithm should look, can you check what im doing wrong? – Dave Jan 23 '21 at 09:19
  • 2
    To answer the question posted, you approach this by turning up your compiler warnings and enable treat-warnings-as-errors with whatever toolchain you're using. Any compiler worth its bits will identify that `function` has clear paths returning no determinate value, yet claims it will return `int`. Related, `int amount = 0;` followed immediately by `while (amount> 0)` is clearly *never* going to enter the outer loop, so you're heading straight to the end of the function where.. yeah, no specified return value. – WhozCraig Jan 23 '21 at 09:21
  • Can you see this https://i.imgur.com/MHHvl6M.png , this is the algorithm, im trying to convert it into code, but im having troubles. – Dave Jan 23 '21 at 09:26
  • I guess that `else i++` should be replaced by `i++` simply – Damien Jan 23 '21 at 09:48
  • @Damien do you might know what this algorithm does by any chance? Whats the purpose of it – Dave Jan 23 '21 at 09:52
  • @alkantra This algorithm calculates the mean of the positive values of the array – Damien Jan 23 '21 at 10:04
  • Does it compile? Where is the definition of `ile` in `if(ile>0){`? Also, are you aware of the consequences of `return sum/amount;` with both `sum` and `amount` being of type `int`? At the moment the code does not make much sense. – zkoza Jan 23 '21 at 12:35

2 Answers2

0

Here is a solution to your problem:

#include <random>
#include <iostream>

void fill(int arr[]); 
int random(int from, int to);

using namespace std;

int main(void)
{
    int arr[10];
    fill(arr);
    for(int i = 0; i<10; i++)
        printf("%d ", arr[i]);
 
    return 0;
}

void fill(int arr[]){
    for(int i=0;i<(*(&arr + 1) - arr);i++){
        arr[i] = random(0, 10);//adjust accordngly
    }
}

int random(int from, int to){
    std::random_device dev;
    std::mt19937 rng(dev());
    std::uniform_int_distribution<std::mt19937::result_type> dist6(from, to); // distribution in range [from, to]
    return dist6(rng);
}

Your problem is you are not generating random numbers your algorithm is generating the same set of numbers! You need a logic to generate random number. Usually they are generated from system time ...

Attribution : https://stackoverflow.com/a/13445752/14911094

Jaysmito Mukherjee
  • 1,467
  • 2
  • 10
  • 29
0

@alkantra, your problem is not generating random numbers. Basically, you are asking your question wrong. If required, it should be separated:

  1. What's this code doing?
  2. How to generate a random sequence?

The algorithm you are trying to achieve is for calculating arithmetic mean (or simply average). If you remember the formula for calculating arithmetic mean you learnt in school, the formula is:

                arithmetic mean = sum/n

where

sum - sum of all numbers (from the given array[] of course)

n - count of the numbers in the given array[]

The purpose of the sum variable is to sum all given numbers, if not equal to 0, and n(in your code amount) just increases for every number added to sum. And in the end the function should return, as the formula says, sum/amount. I could write this code, i.e. the whole program (except for the random()), though it's quite easy, so I'll leave it up to you.

About the random library, I don't know much, but there are may resources on the net, so take your time.

https://www.tutorialspoint.com/cplusplus-program-to-generate-random-number https://www.tutorialspoint.com/rand-and-srand-in-c-cplusplus

Mr.Stark135
  • 1
  • 1
  • 6
  • If sb is not agreed, take a look at the picture, shown by the link the guy shared P.S. Don't look at the code, it's wrong anyway, look at the block diagram on this link https://i.imgur.com/MHHvl6M.png – Mr.Stark135 Jan 25 '21 at 10:11