Thread: problem store random numbers in array

  1. #1
    Registered User
    Join Date
    Jan 2015
    Posts
    26

    problem store random numbers in array

    i cannot print the values stored in the array someone can see the mistake?
    Code:
    #include<stdio.h> #include<math.h> #include<time.h> #include<stdlib.h> double randf(); int main() { double s; s=randf(); printf("%lf", s); return 0; } double randf() { double a = 1; double b = 0; double x[8]; for (int i = 0; i <= 8; ++i) { srand((unsigned)time(NULL)); x[i] = rand()*(a - b) / (double)RAND_MAX + b; } return x[i]; }

  2. #2
    Registered User
    Join Date
    Nov 2011
    Posts
    161
    At what point in your code does the 'for' loop end, and what is 'i' at that point?

  3. #3
    Registered User
    Join Date
    Jan 2015
    Posts
    26
    now i put the return i inside the loop and it returned me only one value

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You should call srand only once, typically near the start of the main function.

    You probably should declare the array in main, then pass a pointer to its first element as an argument to randf (e.g., by passing the array as an argument). After that, use a loop to print the elements of the array in main (or in another function designated for that task).
    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

  5. #5
    Registered User
    Join Date
    Jan 2015
    Posts
    26
    I did what you told me and i still have some error that do not let me run the code.
    Code:
    #include<stdio.h>
    #include<time.h>
    #include<stdlib.h>
    double randf(double *p);
    #define SIZE 8
    int main()
    {
    	double arr[SIZE];
    	randf(&arr[SIZE]);
    
    
    }
    double randf(double *p)
    {
    
    
    	double a = 1;
    	double b = 0;
    	for (int i = 0; i <= 8; ++i)
    	{
    		srand((unsigned)time(NULL));
    		*(p + i) = rand()*(a - b) / (double)RAND_MAX + b;
    
    
    	}
    	return 0;
    
    
    }

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Xavi Camps
    I did what you told me and i still have some error that do not let me run the code.
    If there is an error, then you should tell us what exactly is the error. Saying the vague "some error" is begging for a vague response like "there are things that you need to fix".

    This is wrong:
    Code:
    randf(&arr[SIZE]);
    You're passing a pointer to the element one past the end of the array. Rather, you want to pass a pointer to the first element of the array, so it should be:
    Code:
    randf(&arr[SIZE]);
    or equivalently because an array is converted to a pointer to its first element in this context:
    Code:
    randf(arr);
    I told you that the srand call should be placed near the start of the main function, yet you placed it in the randf function.

    In randf, this is wrong:
    Code:
    i <= 8
    You did well by defining the named constant SIZE, so you should use it:
    Code:
    i < SIZE
    Note the < instead of <= because you start counting from 0.

    Instead of writing *(p + i) write p[i].
    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

  7. #7
    Registered User
    Join Date
    Jan 2015
    Posts
    26
    I did it and it workd perfectly, i add two more functions, one calculate the average value of the array and the other reverse this array, the problem is that the program does not print the reverse matrix.
    Code:
    #include<stdio.h>
    #include<time.h>
    #include<stdlib.h>
    double randf(double *p);
    double average(double *k);
    double reverse(double *j);
    #define SIZE 8
    int main()
    {
    
    
    	double b;
    	srand((unsigned)time(NULL));
    	double arr[SIZE];
    	randf(arr);
    	b = average(arr);
    	printf(" average is equal to %lf\n", b);
    	printf("this is the reverse matrix\n");
            reverse(arr);
    	
    }
    double randf(double *p)
    {
    	double a = 1;
    	double b = 0;
    	for (int i = 0; i < SIZE; ++i)
    	{
    		
    		p[i] = rand()*(a - b) / (double)RAND_MAX + b;
    
    
    		printf("%lf\n", p[i]);
    	}
    	
    	return 0;
    
    
    
    
    }
    double average(double *k)
    {
    	double x = 0;
    	for (int i = 0; i < SIZE; ++i)
    
    
    		x +=(k[i])/SIZE;
    	
    	return x;
    }
    double reverse(double *j)
    {
    	for (int i = SIZE; i < 0; --i)
    	{
    		j[i] = rand()*(1 - 0) / (double)RAND_MAX + 0;
    		printf("%lf\n", j[i]);
    	}
    	return 0;
    }

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is the "reverse matrix"? If you want to reverse the elements of the array, then you should be modifying the existing array element values (or a copy thereof), not calling rand() as if you are populating the array all over 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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 10-30-2012, 12:22 PM
  2. Random numbers into array
    By Gil Carvalho in forum C Programming
    Replies: 15
    Last Post: 05-31-2012, 03:14 PM
  3. Replies: 1
    Last Post: 11-04-2011, 01:16 PM
  4. Filling an array with random numbers
    By euclid in forum C Programming
    Replies: 21
    Last Post: 11-28-2007, 06:53 PM
  5. Unique Random Numbers in 2D Array
    By kssjbr in forum C++ Programming
    Replies: 2
    Last Post: 08-06-2003, 02:45 AM