473,322 Members | 1,417 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

How to populate an array

22
how do u populate an array
Mar 15 '07 #1
12 32236
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
holla
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
2,476 2GB
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
holla
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
3,652 Expert 2GB
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
holla
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
2,476 2GB
then what's the problem with that ......
Mar 15 '07 #8
sicarie
4,677 Expert Mod 4TB
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
2,476 2GB
soorry i don't understand his problem .......
Mar 15 '07 #10
Ganon11
3,652 Expert 2GB
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
4,677 Expert Mod 4TB
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
3,652 Expert 2GB
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

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

Similar topics

5
by: deko | last post by:
I use a For Each... Next loop like this: For Each varFnm In Array("This", "That", "OtherThing", "Foo", "Bar") RunSql ("UPDATE.... bla bla bla) Next But the exact same elements of the Array...
2
by: Anil | last post by:
Hi All, I have a string which has product names, which are seperated by comma. The number of products in the string are random. I want to populate the array using the string, one product per...
1
by: Marcelo | last post by:
Hi, I have to do the following, and would like you suggestions on how I could achieve it the best way: I have a text file in the following format: 12345 54321 98765 56789 (3 spaces...
1
by: Shawn | last post by:
Hi. I have a two dimensional array. Is it possible to fill it with xml data? Thanks, Shawn
3
by: Guy Bloomfield | last post by:
Does anyone know of an easy way to populate an array from a delimited text file when you don't know the number of columns ahead of time? I've been trying all day to use a combination of...
4
by: Sharon | last post by:
hello, I wanted to populate an array with the data from sql table, but not sure how to go about it. This is the array iam using at present, but i dont want to provide the values. Instead i...
10
by: akselo | last post by:
Hi folks, I am working on a routine that will select a sample of parcels from a table. Each parcel belongs to a census tract, and depending on which tract, a certain calculation is applied. The...
5
Kelicula
by: Kelicula | last post by:
Hello all. I have a problem which seems to make no sense to me. Therefore I must be doing something wrong. I am trying to populate an array using split with a regexe. Here is the code (snippet...
0
by: stumpednewbie | last post by:
my code (what I think I want): for (lv=0;lv<20;lv++) { if (kbhit())//look for keypress kp=getch();//assign keypress to variable "kp" SD=(kp);//populate array with wanted digits }
0
by: Marek Dohojda | last post by:
I need to split an output from a bash script and populate an array with it. So I can manipulate and print that later. Unfortunately I can not find a way to do that. so what I have thus far is...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.