-2

hello everybody i m trying to do one-dimensional array fill with C Functions 10 elements random number between 10 and 20 and after average of 10 elements and print to screen i don't know well the functions help...

 int main()  {

       int m[ 10 ]; 
       int i,j;

       for ( i = 1; i < 11; i++ )   
       {
          m[ i ] = i + 0;   
       }

       for (j = 1; j < 11; j++ )    
       {
          printf("Element[%d] = %d\n", j, m[j] );
       }

       return 0;

}
Floris
  • 45,857
  • 6
  • 70
  • 122
  • 1
    Boundaries seem to be difficult today. `int m[10];` declares an array of 10 `int`s meaning you may use `m[0]` up to (and including) `m[9]` **but no further**. No `m[10]`, as your loops attempt to. – Kninnug Nov 28 '13 at 23:07
  • I think you are struggling a bit with language. Are you saying "I want to fill an array of 10 elements with 10 random numbers between 10 and 20, then take the average of those 10 numbers and print the numbers and their average to the screen"? The function you need to look at is `rand`, but you have to scale it (since it produces numbers outside the range you are interested in). – Floris Nov 28 '13 at 23:07
  • array index start 0 in c. see reference rand() and srand() – BLUEPIXY Nov 28 '13 at 23:07
  • use some tutorials and then ask – Christos Papoulas Nov 28 '13 at 23:09
  • no its 10 element random number with {10, 11, 12, 13, 14, 15, 16, 17, 18, 19} to the this 10 element – איוואילו Nov 29 '13 at 09:05
  • "Between 10 and 20" is usually interpreted as "including 10 and 20". You want "between 10 and 19, inclusive". I assume you are OK with nbera being repeated or are you looking for a shuffle (each of the ten numbers exactly once)? – Floris Nov 30 '13 at 01:22

4 Answers4

1

The following should help - it fixes the problem with the loop index (go from 0 to 9, not 1 to 10), and generates random numbers between 10 and 20. There is a small error - using the modulus operator you get very slightly uneven distribution. If you care enough you can create a function that eliminates random numbers above, say, 32758. Then it will be "completely fair".

EDIT I have modified this program so it is split into a number of functions - one that generates the array, another that prints the array, and a third that takes the average of the array. It also includes a custom function that makes a "fair" random distribution. I think your professor will think you had help (and if he googles any phrase in the code he will land on this page). So use this as inspiration, then write your own code. Learning from examples is good; plagiarism is not good.

EDIT Modified the fillArray function. Now uses the space allocated in the main program, rather than creating space in the function itself. It a little bit simpler.

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

// function declarations
int* fillArray(int* a, int n, int llim, int ulim);
float arrayAverage(int *a, int nElements);
void arrayPrint(int *a, int n);
int fairRand(int, int);

int main(int argc, char* argv[])  {

   int m[10];
   float av;
   m = fillArray(m, 10, 10, 20); // note that 'm' is a pointer (int*) to the array m[]
   av = arrayAverage(m, 10);
   arrayPrint(m, 10);
   printf("The average is %.2f\n", av);
   return 0;
}

int* fillArray(int *a, int n, int llim, int ulim) {
  // given an array a with size n
  // fill each element in the array with a random element
  // between llim and slim (inclusive)
  int i;

   for ( i = 0; i < n; i++ )
   {
      a[ i ] = fairRand(llim, ulim); // calling our user-defined function
   }
   return a;
}

float arrayAverage(int *a, int n) {
  // returns the sum of values in a[n] divided by n
  int j;
  double s=0.0;
   for (j = 0; j < n; j++ )
   {
     s += a[j];
   }
   return (float)(s / (double) n);

}

void arrayPrint(int *a, int n) {
  // prints each element in the array m[n]
  int  j;
   for (j = 0; j < n; j++ )
   {
      printf("Element[%d] = %d\n", j, a[j] );
   }
}

int fairRand(int a, int b) {
  // generates fair random number between a and b, inclusive
  int diff = b - a + 1;
  int r;
  int largest = diff * (RAND_MAX / diff);
  // this is the "magic" line: keep generating random numbers
  // until the number generated fits in the interval that has equal number
  // of each possible value after the modulo operation:
  while( (r = rand()) >= largest) {
    // keep going around...
  } ;

  // we now have a "fair" random number r
  return a +  r % diff;
}
Floris
  • 45,857
  • 6
  • 70
  • 122
  • m[ i ] = 10 + rand() % 11; why we giving % 11 ? – איוואילו Nov 29 '13 at 09:16
  • 10, 11, 12 ... 20 is 11 numbers in all (put another way the expression `mod 11` yields numbers from 0 to 10 inclusive) – Floris Nov 29 '13 at 14:37
  • `void main (void)` i have to use for function ?!?! – איוואילו Nov 29 '13 at 15:11
  • actually what is function mean no any explanation for that if codes starting with `void main (void)` this is mean one function what different with `int main()` ?? – איוואילו Nov 29 '13 at 15:15
  • The correct declaration of the `main()` function is that it returns an `int`. In fact, the full declaration ought to be `int main(int argc, char* argv[])` so that you can pass a variable number of (string) arguments to the function but I did not want to confuse you further. See edit for how to use the function that I defined. It is OK to declare `int main()` or `int main(void)`; see [this earlier answer](http://stackoverflow.com/a/9356660/1967396) (follow the links from there) for further details on what/when/how/why. – Floris Nov 29 '13 at 15:31
  • this is my home work with different numbers. i did this way but my professor said wrong :) and replay use functions what kind of functions ?!?! this code correct and working your post is true ! – איוואילו Nov 29 '13 at 15:42
  • I assume your professor wants you to write a function `fillArray(10, 10, 20)` that returns an array with 10 numbers between 10 and 20, and another function `average(arrayName, 10)` that returns the value of the average. I will write an example of how this would be done. But I have not seen your assignment and cannot read the mind of your professor... – Floris Nov 29 '13 at 15:47
  • can i write here real question is it problem ? – איוואילו Nov 29 '13 at 16:30
  • 1
    If you are going to post an actual homework question, you have to be prepared to show what you have done and **very specifically** where you are struggling. Don't make it sound like "do my homework for me" and you will be OK. Se for example http://whathaveyoutried.com to get some guidance. – Floris Nov 29 '13 at 18:10
  • the wolf's neck is thick. i hate giving my project to somebody as i m doing as this is my job, computing. actually i don't know prof. what want from me this code is working, correct. is there any second way to do this function ? as `fillArray` i m not good in c/#/++ languages logical stuff – איוואילו Nov 29 '13 at 18:33
  • "The wolf's neck is thick because he does everything himself" - but the wolf hunts in packs, and the cubs learn from Akela. I changed the `fillArray` function a little bit - you now pass the address of the array to be filled into it (rather than allocating space for it inside the function). Beyond this I am out of ideas for how to help you. Learning computer languages takes time, effort, and lots of mistakes - there is no magic pill. Once you learn to "think like a computer" (that is - understand that the computer does exactly what you ask, which can be good or bad) things will get better. – Floris Nov 29 '13 at 19:29
  • @ floris do you have any social media account ? – איוואילו Nov 29 '13 at 19:53
  • I don't give social media info out. You can reach me on SO@ my handle name dot us if you want to take this conversation offline – Floris Nov 29 '13 at 22:51
  • SO has a chat room, specifically for this purpose. (@Floris, I'm tempted to +1 you just for *trying* to help.) – Jongware Nov 30 '13 at 00:58
  • @jongware - need certain amount of rep for chat ; OP does not have it. And thanks! – Floris Nov 30 '13 at 01:19
  • I need to learn low level programming as basic c and I think @floris have good experience with c this way I ask the social media for follow maybe WordPress post I don't know how to start learn c just this. before I interest with web based languages. – איוואילו Nov 30 '13 at 01:57
  • @ivayloivaylov I don't have any blogs about programming but there are tons of good ones. If you just search this site for questions tagged `c` with 10+ votes you will learn a lot. After that - just start trying stuff. There are all kinds of "programming challenges" - it depends on how you want to use what you learn. For specific applications there may be other languages than C - it is powerful but "very close to the computer". Coming from web programming that will be a shock. – Floris Nov 30 '13 at 02:47
  • @Floris professor said use 3 user defined function what is this ? – איוואילו Dec 02 '13 at 11:02
  • Well this code has FOUR user defined functions. A function is a block of code that does something - calculate, store, print out,... It usually returns a value (although it can return "nothing"- we call that `void`) `fillArray` etc above are user (i.e. me) defined - they keep the `main` program clean – Floris Dec 02 '13 at 15:14
  • @ivayloivaylov please delete your comment I do not want easily readable email addresses online to fight spam. Will be in meetings all day so unlikely to read / respond in next few hours. You need to get a couple of good books I cannot become your distance learning coach. – Floris Dec 02 '13 at 16:30
0
#include <stdlib.h>
...
for ( i = 1; i < 11; i++ )   
    m[ i ] = 10 + rand() % 11;   

int sum = 0, size = 10;
for (j = 0; j < size; j++ )    
{
    sum += m[j];
    printf("Element[%d] = %d\n", j, m[j] );
}

double average = (double) sum / (double) size;
printf("Average = %lf\n", average);
yasen
  • 1,250
  • 7
  • 13
0

The code Bit Fiddling Code Monkey posted in his answer should work. You question also included printing it out so I'll just rewrite his code fitting in your context:

int main()  
{
    int m[ 10 ]; 
    int i,j;
    int sum = 0, average;

    srand(time(NULL));              // Randomize seed

    for(int i = 0; i < 10; i++)
    {
        m[i] = (rand() % 11) + 10; // Get a random number in the range 10 to 20
        sum += m[i];               // Get sum
    }

    average = sum / 10;            // Get average

    for (j = 0; j < 10; j++ )    
    {
        printf("Element[%d] = %d\n", j+1, m[j] );
    }
    return 0;
}
tobypls
  • 839
  • 1
  • 8
  • 21
0

you can use the rand function for this. this is a small clarification on how to decide the random number range. we have good solutions above. so i don't want to write the code here.

if you want to generate number between 10-20 you can use the following formula.

a=10 b=20 value = a + rand()%(b-a+1)

this will give you

10 + rand() % 11;

hope this helps

Tharanga Abeyseela

Tharanga Abeyseela
  • 3,255
  • 4
  • 33
  • 45