-1

I am a beginner and just started learning so i would appreciate any information and guidance.

I want to generate an array of length 15 containing random numbers between 0 and 10. I want to prompt the user for a number in that range, and print out the number of times that number appears. I then want to print out the array with an asterisk(*) indicating each instance of that number in that array

Here is the code I am working on:

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

int main(){
    srand(time(NULL))
    int r=rand(0)%10

    int num; /*user's input*/
    int r;
    numbers[15]={r,r,r,r,r,r,r,r,r,r,r,r,r,r,r}
    printf("Input a number between 0 and 10. \n")
     for(num= ; num ; num++)
         printf("*")

    return 0;

}
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
user1049876
  • 117
  • 1
  • 3
  • 18
  • Do you know how to create an array? Do you know how to assign value to each element in an array? Do you know how to get input from a user? Do you know how to compare two values for equality? If you know each of those things separately, you know how to solve your problem. – Some programmer dude Jul 01 '16 at 17:28
  • My recommendation is that you break down the task into smaller steps. Take each requirement, and break it down, and continue to break down each step until it's not possible any more. Then just do each step one at a time, until you have a complete program. For example, the first requirement seems to be to create an array of 15 elements. That can't really be broken down any more, so create an array of 15 elements. Then continue like that, one step at a time, until your program is done. – Some programmer dude Jul 01 '16 at 17:31
  • @JoachimPileborg This is my first time learning C, so I do not know how to do that. I have been looking EVERYWHERE online. I would appreciate any help you can provide. – user1049876 Jul 01 '16 at 17:31
  • 1
    Then you should really start by [finding a good beginners book](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) and start reading. – Some programmer dude Jul 01 '16 at 17:32
  • @JoachimPileborg I have a beginner's book but I am not able to find information on the said issue. – user1049876 Jul 01 '16 at 17:33
  • Then do like I also said: Take one step at a time, reading the related chapters in the book and then try to implement each little step. Learn to crawl before you learn to walk, and learn to walk before you learn to run, and so on. Small steps is the way to go. – Some programmer dude Jul 01 '16 at 17:46
  • @JoachimPileborg Ok, but if you don't mind, I have a question on an error being received. "initialization makes pointer from integer without a cast. 'int *' numbers[15]={r,r,r,r,r,r,r,r,r,r,r,r,r,r,r}" – user1049876 Jul 01 '16 at 17:48
  • @JoachimPileborg I am not the only one with this problem. In the answers section, ivyraveneve in the answers section said she needs help, too. – user1049876 Jul 01 '16 at 17:53

2 Answers2

3

I want to generate an array of length 15 containing random numbers between 0 and 10

  • create an array of size 15
  • use a for loop to populate the array using rand() function.

Note :

  • use rand()%9+1 to return a random number between 0 and 10 (0,10 not included ) and don't send any arguments into rand() function.
int array[15]; //array of size 15

for(int index=0; index<15; index++)
{
    array[index] = (rand()%9)+1; //randomly generates a number between 0 and 10
}

prompt the user for a number in that range and print out the number of times that number appears

  • most of what you want to achieve can be achieved by using for loop

here, we scan a number from user and store in number and loop over array to find the number of times number appears and each time it appears we increase the value of count which is initially set to 0.

int number,count=0;
printf("enter a number between 0 and 10 : ");
scanf("%d",&number); //scanning user prompted number

for(int index=0; index<15; index++)
{
    if(array[index]==number) //checking if array element is equal to user's input
        count++; //if yes increase the count
}
printf("the number appeared %d times\n",count); //printing number of times the number appeared

I then want to print out the array with an asterisk(*) indicating each instance of that number in that array

for(int index=0; index<15; index++)
{
    if(array[index]==number)
        printf("* ");
    else
    printf("%d ",array[index]);
}

So altogether your code would be :

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

int main(void)
{
    srand(time(NULL));

    int array[15];

    //populating array with random elements between 0 and 10
    for(int index=0; index<15; index++)
    {
        array[index] = (rand()%9)+1;
    }

    int number,count=0;

    //scanning user prompted input
    printf("enter a number between 0 and 10 : ");
    scanf("%d",&number);

    //printing number of times the user entered number appeared
    for(int index=0; index<15; index++)
    {
        if(array[index]==number)
            count++;
    }
    printf("the number appeared %d times\n",count);

    // printing the array with * in place of user entered input
    for(int index=0; index<15; index++)
    {
        if(array[index]==number)
            printf("* ");
        else
        printf("%d ",array[index]);
    }

}
Community
  • 1
  • 1
Cherubim
  • 5,287
  • 3
  • 20
  • 37
2
int main()
{
    srand(time(NULL))
    ....    
    for(i = 0; i < 15; i++)
    {
        numbers[i] = (rand() % 9) + 1;
    }
    return 0;
}
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97