By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,131 Members | 1,812 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,131 IT Pros & Developers. It's quick & easy.

How to populate an array

P: 22
how do u populate an array
Mar 15 '07 #1
Share this Question
Share on Google+
12 Replies


stealwings
P: 34
how do u populate an array
take a look at this page it will help you to find out array and strings
Mar 15 '07 #2

P: 22
take a look at this page it will help you to find out array and strings
i am not sure if this is how u populate

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. rand()
  5. int array(*array size)
  6.  
  7. main ()
  8. {
  9.     int i,j;
  10.     {
  11.     for (i =0,i >array size,++i)
  12.         array[i] = rand()%100
  13.     return *array}
  14. }
but there is a syntax error.could u correct me
Mar 15 '07 #3

dmjpro
100+
P: 2,476
at first i don't understand what r u trying to do ...........

plz report me the error u get after compilation .......

welcome
Mar 15 '07 #4

P: 22
at first i don't understand what r u trying to do ...........

plz report me the error u get after compilation .......

welcome
i am tying to write a function that can populate an array for integers btw 0 & 99
and i get a syntax declaration error
Mar 15 '07 #5

Ganon11
Expert 2.5K+
P: 3,652
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. rand()
  5. int array(*array size)
  6.  
  7. main ()
  8. {
  9.     int i,j;
  10.     {
  11.     for (i =0,i >array size,++i)
  12.         array[i] = rand()%100
  13.     return *array}
  14. }
This code has several errors. First, you have a call to rand() outside of main. This is a value returning function, but you don't assign it to anything, so what is the point? Perhaps you were trying to use srand in order to get unique random numbers, but this call should be inside main, in your first line, and pass an integer argument to the function.

Next, by saying int array(*array size), you are trying to declare a function prototype, but *array is not a valid type, so this function won't work.

You declare i and j, but never use j - only i in the for...loop. Also, the loop header is using commas ',' where it should be using semicolons ';', and it is set to continue while i is greater than the array size, which means it shouldn't execute at all (since 0 is not greater than the array size). In addition, you didn't define array size! It even has a space in the middle, so I'm sure your compiler will complain about that.

Finally, you are returning *array, which will return the first element in the array - but you have declared main() without specifying the return type, so the compiler is not expecting a return statement.

Try the following code to populate an array:

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. int main() {
  4.    int i;
  5.    int array[10];
  6.    for (i = 0; i < 10; i++) {
  7.       array[i] = i;
  8.       printf("%d ", array[i]);
  9.    }
  10.    return 0;
  11. }
Mar 15 '07 #6

P: 22
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. rand()
  5. int array(*array size)
  6.  
  7. main ()
  8. {
  9.     int i,j;
  10.     {
  11.     for (i =0,i >array size,++i)
  12.         array[i] = rand()%100
  13.     return *array}
  14. }
This code has several errors. First, you have a call to rand() outside of main. This is a value returning function, but you don't assign it to anything, so what is the point? Perhaps you were trying to use srand in order to get unique random numbers, but this call should be inside main, in your first line, and pass an integer argument to the function.

Next, by saying int array(*array size), you are trying to declare a function prototype, but *array is not a valid type, so this function won't work.

You declare i and j, but never use j - only i in the for...loop. Also, the loop header is using commas ',' where it should be using semicolons ';', and it is set to continue while i is greater than the array size, which means it shouldn't execute at all (since 0 is not greater than the array size). In addition, you didn't define array size! It even has a space in the middle, so I'm sure your compiler will complain about that.

Finally, you are returning *array, which will return the first element in the array - but you have declared main() without specifying the return type, so the compiler is not expecting a return statement.

Try the following code to populate an array:

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. int main() {
  4.    int i;
  5.    int array[10];
  6.    for (i = 0; i < 10; i++) {
  7.       array[i] = i;
  8.       printf("%d ", array[i]);
  9.    }
  10.    return 0;
  11. }
it does work but i need to populate an array with random integers btw 0 and 99
Mar 15 '07 #7

dmjpro
100+
P: 2,476
then what's the problem with that ......
Mar 15 '07 #8

sicarie
Expert Mod 2.5K+
P: 4,677
it does work but i need to populate an array with random integers btw 0 and 99
You have the modulus going between 0 and 100, not 99. Try changing that and see if it helps....
Mar 15 '07 #9

dmjpro
100+
P: 2,476
soorry i don't understand his problem .......
Mar 15 '07 #10

Ganon11
Expert 2.5K+
P: 3,652
You have the modulus going between 0 and 100, not 99. Try changing that and see if it helps....
Using % 100 will actually restrict the result to values between 0 and 99, inclusive. Suppose the rand() function returned any number from 0 to 99 on its own - then num % 100 is still num. If the number returned is 100, then 100 % 100 is 0, not 100. This pattern repeats for every 100 numbers, so using % 100 will work as needed.

holla: I wrote that short snippet to show you how to populate an array, but I specifically did not write the code to completely solve your problem. The code I provided initializes the array[i] value to i. For the program to work as you want, you need only replace the right hand portion with the necessary code to create a random number between 0 and 99, which you had correctly in your own code.
Mar 15 '07 #11

sicarie
Expert Mod 2.5K+
P: 4,677
Using % 100 will actually restrict the result to values between 0 and 99, inclusive. Suppose the rand() function returned any number from 0 to 99 on its own - then num % 100 is still num. If the number returned is 100, then 100 % 100 is 0, not 100. This pattern repeats for every 100 numbers, so using % 100 will work as needed.
Dang, this is what happens when I try to quit coffee... :(
Mar 15 '07 #12

Ganon11
Expert 2.5K+
P: 3,652
Don't sweat it - I messed up pretty badly here, so it happens to all of us.

Even me! :-O *surprise*
Mar 15 '07 #13

Post your reply

Sign in to post your reply or Sign up for a free account.