Search for numbers! NEED HELP!

0 Elias Zaguri · October 6, 2015
So my task is to create a program that generate 10 random integer numbers into an array and write them on the screen. After that the program is supposed to ask the user for a number, count how many times that integer number is generated and write the result. The 10 numbers is supposed to be saved into an array and the user is supposed to use the program again without shutting it down.

I need to create atleast 3 functions (more if i want to):

* One that fills the array with random numbers

* One that handles the search, No input or output in this function, It is supposed to return number of occurrences.

* main() function that only handles input and write the result.

Example:
Number: 7
Number: 9
Number: 8
Number: 4
Number: 3
Number: 9
Number: 1
Number: 2
Number: 3
Number: 3

What number should i search for: 9
Number 9 occur 2 times.

Do you want to start again (y/n)?


Here's my code so far. I need tip on how to fix it!



#include
#include
/* function to generate and return random numbers */
int  getRandom()
{
  int r[10];
  int i;
  srand(time(0));
  for ( i = 0; i < 10; ++i)
  {
     r = rand()%10+1;

   printf( "r[%d] = %d\n", i, r);

  }

  return r;
}

int antal_forekomster()
{
    int forekomster =0;

    for (int i = 0; i<10; i++)
    {
        if (arr == nummer)
            forekomster++;
    }

    reutrn forekomster;
}


/* main function to call above defined function */
int main ()
{
 int rndm = getRandom();
  int antalet= antal_forekomster();
   int i;
   int nummer;
   int nummer==i;

   printf("Vad ska jag söka efter?: \n");
   scanf(" %d", i);
   printf("Antal förekomster: %d", nummer);


   return 0;
}




Post a Reply

Replies

Oldest  Newest  Rating
0 Jon Z. · October 7, 2015
ok first of all you need to store each number in the array
r[i] = rand...

then in order to be able to access the array from different functions, you should make it global, so define it outside the functions.

then to get the interface to work the way you want, you should make a loop in your main function. something like this:
main(){
int num;
char ans;
while(1){
getRandom();
printf("What number should i search for: ");
scanf("%d", &num);
antal_forekomster(num); //you should make this function take an int
printf("Do you want to start again (y/n)? ");
scanf(" %c", &ans);
if(ans == 'n'){
break; //exits loop
}
}

}
  • 1

C

107,258 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator