Thread: Need help!! Find number in an array..

  1. #1
    Registered User
    Join Date
    Sep 2015
    Posts
    18

    Need help!! Find number in an array..

    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!



    Code:
     
    
     #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[i] = rand()%10+1;
    
     
    
        printf( "r[%d] = %d\n", i, r[i]);
    
     
    
       }
    
     
    
       return r;
    
     }
    
     
    
     int antal_forekomster()
    
     {
    
         int forekomster =0;
    
     
    
         for (int i = 0; i<10; i++)
    
         {
    
             if (arr[i] == 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;
    
     }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You need to look into how to pass parameters to your functions.

    Specifically, an array declared in main to hold the information while the other functions are called.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2015
    Posts
    18
    Ill do that, thanks for the tip, I didnt know what to search for, google is to big to find something specific if you don't know what you're searching for!

  4. #4
    Registered User
    Join Date
    Sep 2015
    Posts
    18
    Got another question if ur up for it.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <stdbool.h>
    
    
    
    
    /*funktion som ger programet slump siffra mellan 1-100*/
    
    int random(void)
    {
        int ra;
        ra = rand() %100+1;
        return ra;
    }
    
    int main()
    {
    
       srand(time(0));
       int r, guess;
        r = random();
       int answer;
    
       int counter=0;
       do
    {
    
       do
    {
        start:
       printf("Guess a number between 1-100: \n");
       scanf(" %d", &guess);
       if (guess == r)
       {
        printf("That's the right number \n");
       }
       else if (guess > r)
       {
        printf("Your guess is too high \n");
       }
       else if (guess <r)
       {
           printf("Your guess is too low \n");
       }
       counter++;
    
    
       }
       while (r !=guess);
    
       printf("Congratulations it took you %d tries \n", counter);
    
    
    
      printf("Again? 1 for yes 0 for no \n");
      scanf(" %d",&answer);
       r = random();
    
      }
    
    while ( answer != 0 );
    
       return 0;
    }
    I get the program to do what I want. The problem is that when I want to play again, the count continues from previous game. Any tip how to fix that?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Choose where you set counter to 0
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Sep 2015
    Posts
    18
    So i tried to fix the code and come up with this.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    
    int * random( );
    int antal_forekomster(int arr[10], int nummer);
    
    
    int main()
    {
       int * p;
       int i;
       int nummer;
       int antal;
       p = random();
       printf("Vad ska jag söka efter: \n");
       scanf(" %d",&nummer);
    
    
      printf("Antal förekomster: " + antal_forekomster(p, nummer));
    
    
    
        return 0;
    }
    
    
    int * random( ){
     static int arr[10];
     int k;
        srand( (unsigned)time( NULL ) );
        for (k=0; k<10; k++){
    
    
        arr[k]=rand()%10+1;
        printf( "r[%d] = %d\n", k, arr[k]);
    
        }
    return arr;
    }
    
    int antal_forekomster(int arr[10], int nummer)
    {
        int forekomster = 0;
        int i;
        for (i=0;i<10;i++)
        {
            if (arr[i] == nummer)
                forekomster++;
        }
    
        return forekomster;
    }
    When i run the code it doesnt count the occurence of the number the user input. don't know how to make it call for the function i made to count!
    Need some more pointers, thanks

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Elias Zaguri
    When i run the code it doesnt count the occurence of the number the user input. don't know how to make it call for the function i made to count!
    Need some more pointers, thanks
    You don't need more pointers, you just need them in the right places

    Let's list what you are supposed to do:
    • Generate 10 random integers and save them into an array. Let's call this array random_numbers.
    • Print the elements of random_numbers to the screen.
    • Ask the user for a number. Let's call this number x.
    • Count how many times the value of x matches an element in random_numbers.
    • Print the number of times the value of x matches an element in random_numbers.
    • Allow the user to run the above again without having to re-run the program.


    If I were you, I would divide most these tasks into functions, and if these functions are too complex, I would further divide them into helper functions that do one thing and do it well. So, I might declare:
    Code:
    void generate_random_numbers(int random_numbers[], size_t n);
    void print_random_numbers(const int random_numbers[], size_t n);
    int get_user_input(void);
    int count_matching_numbers(const int random_numbers[], size_t n, int x);
    Notice that three of these functions have a pointer parameter named random_numbers. You can declare the random_numbers array in the main function as having 10 elements, then pass this array as well as the numbers of elements (n) to these functions.

    With the help of count_matching_numbers, printing the number of times the value of x matches an element in random_numbers is easy: you just make use of the return value and print it in the main function.

    Do all these first and make sure that it works for a single run of the program. Once you are certain it works, modify the main function to add a loop to allow the user to run the above again.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Sep 2015
    Posts
    18
    Quote Originally Posted by laserlight View Post
    You don't need more pointers, you just need them in the right places

    Let's list what you are supposed to do:
    • Generate 10 random integers and save them into an array. Let's call this array random_numbers.
    • Print the elements of random_numbers to the screen.
    • Ask the user for a number. Let's call this number x.
    • Count how many times the value of x matches an element in random_numbers.
    • Print the number of times the value of x matches an element in random_numbers.
    • Allow the user to run the above again without having to re-run the program.


    If I were you, I would divide most these tasks into functions, and if these functions are too complex, I would further divide them into helper functions that do one thing and do it well. So, I might declare:
    Code:
    void generate_random_numbers(int random_numbers[], size_t n);
    void print_random_numbers(const int random_numbers[], size_t n);
    int get_user_input(void);
    int count_matching_numbers(const int random_numbers[], size_t n, int x);
    Notice that three of these functions have a pointer parameter named random_numbers. You can declare the random_numbers array in the main function as having 10 elements, then pass this array as well as the numbers of elements (n) to these functions.

    With the help of count_matching_numbers, printing the number of times the value of x matches an element in random_numbers is easy: you just make use of the return value and print it in the main function.

    Do all these first and make sure that it works for a single run of the program. Once you are certain it works, modify the main function to add a loop to allow the user to run the above again.


    Tried to listen to your instructions. But I think my program is worse now, I dont really know how to call the functions in the right way i suppose,

    Here's the code now

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    void generate_random_numbers(int random_numbers[], size_t n);
    void print_random_numbers(const int random_numbers[], size_t n);
    int get_user_input(void);
    int count_matching_numbers(const int random_numbers[], size_t n, int x);
    
    int main()
    {
     int numbers,write;
     int random_numbers[10];
     int input;
     int count;
    
     numbers = generate_random_numbers();
     random_numbers = numbers;
     write = print_random_numbers();
     input = get_user_input();
     count = count_matching_numbers();
    
     printf("Number of occurences: \n", );
    
    
        return 0;
    }
    
    void generate_random_numbers(int random_numbers[], size_t n);{
     int random_numbers[10];
      int k;
      int rturn;
        srand((unsigned) time( NULL ) );
        for (k=0; k<10; k++){
    
        random_numbers[k] = rand()%10+1;
        rturn= random_numbers[k];
        }
    return rturn;
    }
    
    
    void print_random_numbers(const int random_numbers[], size_t n);
    {
    
    printf("r[ %d] = %d \n",&random_numbers);
    
    return 0;
    }
    
    
    int count_matching_numbers(const int random_numbers[], size_t n, int x);
    {
        int x = 0;
        int i;
        for (i=0;i<10;i++)
        {
            if (random_numbers[i] == x)
                x++;
        }
    
        return x;
    }
    
    
    int get_user_input(void);
    {
        int x;
    
        printf("What number do you want to check: \n");
        scanf(" %d",&x);
    
    
    return x;
    }
    Can u give me some more tip on how to fix it? Thanks

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Let's start with generate_random_numbers:
    • This:
      Code:
      void generate_random_numbers(int random_numbers[], size_t n);{
      has a stray semi-colon towards the end: you copied my suggested function prototype, but forgot to remove the semi-colon.
    • This is wrong:
      Code:
      int random_numbers[10];
      random_numbers is a parameter. You should make use of it, not declare a local array.
    • This is unnecessary:
      Code:
      int rturn;
      The function will generate random numbers and store them in the array from the caller using the pointer named random_numbers.
    • This is in a wrong place:
      Code:
      srand((unsigned) time( NULL ) );
      The call to srand should go near the start of the main function instead as it only needs to be called once.
    • This is not good as it hard codes with a magic number 10:
      Code:
      for (k=0; k<10; k++){
      You should make use of the parameter named n.
    • This is unnecessary, as noted earlier:
      Code:
      rturn= random_numbers[k];
    • This is wrong:
      Code:
      return rturn;
      The function is declared as having a return type of void, so it should not have a return statement, or if it does have a return statement, it would just be:
      Code:
      return;

    Furthermore, let's look at how you called the function in main:
    Code:
    numbers = generate_random_numbers();
    This must be wrong because generate_random_numbers does not return a value and takes two arguments. You tried to store its return value and passed no parameters.

    Now, let's examine print_random_numbers: you need to print the elements of the array.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Sep 2015
    Posts
    18
    I got all the functions except the search function the work.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    
    void generate_random_numbers(int random_numbers[]);
    int count_matching_numbers(int random_numbers[], size_t n, int x);
    
    
    int main(){
    int random_numbers[10];
    int num_occ;
    
    srand((unsigned) time( NULL ) );
    
    generate_random_numbers(random_numbers);
    
    print_random_numbers();
    
    int x;
    
    printf("What number should I search for: \n");
    scanf(" %d", &x);
    
    num_occ = count_matching_numbers(random_numbers,10, random_numbers[]);
    
    
    printf("Number %d occurs %d times \n",x ,num_occ );
    
    return 0;
    }
    
    
    void generate_random_numbers(int random_numbers[]){
    
     int k;
     for (k=0; k<10; k++)
    
        random_numbers[k]= rand()%10+1;
    
    
    
    }
    
    void print_random_numbers(int random_numbers[], size_t n){
    
    generate_random_numbers(random_numbers);
    
    int i;
    
    for (i=0;i<10;i++)
    
        printf("Number: %d\n", random_numbers[i]);
    
    
    
    }
    
    
    
    
    int count_matching_numbers(int random_numbers[], size_t n, int x){
    
    generate_random_numbers(random_numbers);
    
    int count = 0;
        int i;
        for (i=0;i<10;i++)
        {
            if (random_numbers[i] == x){
                count++;
            }
        }
    
        return count;
    }

    How do i fix the problem and how does the code look?

  11. #11
    Registered User talahin's Avatar
    Join Date
    Feb 2015
    Posts
    51
    Why are you generating a new list of random numbers in every function call?
    Code:
    void print_random_numbers(int random_numbers[], size_t n){
    
    generate_random_numbers(random_numbers);
    Code:
    int count_matching_numbers(int random_numbers[], size_t n, int x){
    
    generate_random_numbers(random_numbers);

  12. #12
    Registered User
    Join Date
    Sep 2015
    Posts
    18
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    
    void generate_random_numbers(int random_numbers[]);
    int count_matching_numbers(int random_numbers[], size_t n, int x);
    
    
    int main(){
    int random_numbers[10];
    int num_occ;
    
    srand((unsigned) time( NULL ) );
    
    generate_random_numbers(random_numbers);
    
    print_random_numbers();
    
    int x;
    
    printf("What number should I search for: \n");
    scanf(" %d", &x);
    
    num_occ = count_matching_numbers(random_numbers,10, x);
    
    
    printf("Number %d occurs %d times \n",x ,num_occ );
    
    return 0;
    }
    
    
    void generate_random_numbers(int random_numbers[]){
    
     int k;
     for (k=0; k<10; k++)
    
        random_numbers[k]= rand()%10+1;
    
    
    
    }
    
    void print_random_numbers(int random_numbers[], size_t n){
    
    
    int i;
    
    for (i=0;i<10;i++)
    
        printf("Number: %d\n", random_numbers[i]);
    
    
    
    }
    
    
    
    
    int count_matching_numbers(int random_numbers[], size_t n, int x){
    
    
    
    int count = 0;
        int i;
        for (i=0;i<10;i++)
        {
            if (random_numbers[i] == x){
                count++;
            }
        }
    
        return i;
    }
    Doesn't matter what number I tell the program to search for, everytime no matter what number I put in, the program says that the number occurs 10 times. Why is that?

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Notice that at the end of count_matching_numbers, you return i instead of count.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [HELP] find closes number from array??
    By Fromar123 in forum C++ Programming
    Replies: 2
    Last Post: 11-25-2012, 01:33 PM
  2. Replies: 5
    Last Post: 03-03-2011, 04:23 PM
  3. Arrays, how to find if some number is the element of an array.
    By InvariantLoop in forum C++ Programming
    Replies: 14
    Last Post: 03-18-2006, 02:43 AM
  4. Replies: 2
    Last Post: 08-03-2003, 10:01 AM
  5. Can't find the lowest number in array?
    By Nate2430 in forum C++ Programming
    Replies: 1
    Last Post: 11-20-2001, 10:21 AM