Thread: problem with rand() in pointers

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    3

    problem with rand() in pointers

    Hi Guys,

    I am trying to enter a rand() in my code below, but it is causing a problem. It says 'a' not intialized.

    Since 'a' is a pointer I didn't think I had to have it equal to anything. I am a beginner at this so maybe I am missing something along the code......

    Code:
    // Program to process integers inputed by user in acending order and showing the number of times each number appears
    // The use of rand() to investigate the random output and calloc to assign allocate memory for these integer inputed by user.
    
    #include <stdio.h>
    #include <stdlib.h>
    
    
    
    int main (void)
    
    {
    	
    	int *a;
    	int i = 0, j=0, n, freq = 0;// input and increment variables
    	
    
    
    	
    	printf("Enter numbers from 1 to 10:");// input from user
    	scanf_s("%d", &n);
    
    		
    	for(i = 0; i <= 7; i++)
    	{
    		*(a+i) = rand()%10 + 1;//creates random array(instead of scanf which allocates a memory to an array elements)
    	
    	}
    	for (j =1;j <= 10; j++)// prints values inside of the array from 1 - 10
    
    		a = calloc(n, sizeof(int));// allocates memory for pointers, as random numbers can be entered by users
    	
    		
    	{
    
    	for ( i = 0; i <= 7; i++)//fills array - loops around until gone through all 8 elements
    	{ 
    		if (j == *(a+i)) //searches array for no. 1 to 10 and assigns value (j) to a[i]
    		{
    		++freq;//count of no of time a particular no. comes up
    		}
    		
    	}
        
    	if(freq!=0)
    	printf("%d occurs %d times\n", j, freq);// output
    	freq = 0;
    	}
    	
    	
    	
    return 0;
    }

    This is the actual assignment:

    (Im not after the answer, just thought it might be useful plus im beyond frustrated...)


    Write a program that reads n integers, in the range 1 to 10, into an array, then prints on a separate line the value of each distinct element along with the number of times it occurs. The values should be printed in descending order. Suppose, for example that you input the values

    1 3 3 1 5 5 3

    as the elements of your array. Then your program should print

    5 occurs 2 times
    3 occurs 3 times
    1 occurs 2 times

    Use your program to investigate the output of rand(). First use rand() to generate an array of 100 random integers in the range 1 to 10. To generate an integer in the range 1 to 10, use the following code

    rand() % 10 +1

    Rewrite your program to make use of calloc, allow the user to enter the number of random integers to be generated and use this to dynamically create the array.

    Remember: to use calloc in C, either name your file filename.c or cast the result of calloc using the cast (int*).

    Finally rewrite your program so that it uses pointers instead of array indices. Your final version should contain no ‘square’ brackets (‘[‘,’]’).

    You only need to submit this final version of your code.

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Code:
    	for(i = 0; i <= 7; i++)
    	{
    		*(a+i) = rand()%10 + 1;//creates random array(instead of scanf which allocates a memory to an array elements)
    	
    	}
    	for (j =1;j <= 10; j++)// prints values inside of the array from 1 - 10
    
    		a = calloc(n, sizeof(int));// allocates memory for pointers, as random numbers can be entered by users
    	
    		
    	{
    Uhm, you first should allocate memory and then assign it values!

    But from the comments I think you have the concept wrong. Pointers are variables that store memory addresses of other variables. When you do
    Code:
    calloc(n, sizeof(int));
    you allocate (reserve) a memory block equal to n*sizeof(int) bytes. Well, calloc() returns the memory address of the first element of the memory block. So when you do
    Code:
    a = calloc(n, sizeof(int));
    Then you store in "a" the memory address of the first element of the memory block allcoated. You can think memory blocks as arrays, same thing.

    Now, when you dereference a pointer (with *) you read the value of the memory which its address is stored in the pointer. So if you do
    Code:
    *a = 0;
    Then you assign the value 0 on the memory address that is stored in "a". You need to have that memory allocated before you can write anything in it.

    So, when you dereference an unitialized pointer then you will try to write/read on a random memory location (somewhere in Texas probably). Which isn't good. The OS will not allow you to do so (since that memory can be reserved by another program for example).

    Dunno if you get the concept now. In any case, allocate first memory, assign the memory to a pointer and then write on where the memory points.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by jugs View Post
    I am trying to enter a rand() in my code below, but it is causing a problem. It says 'a' not intialized.

    Since 'a' is a pointer I didn't think I had to have it equal to anything. I am a beginner at this so maybe I am missing something along the code......
    The compiler is telling you that is must be initialised to point to something, and if that isn't a big enough hint, I'll tell you that it needs to be initialised to point to something before you dereference it. Always listen to your compiler.

    Most of your comments are very wrong:
    creates random array(instead of scanf which allocates a memory to an array elements)
    That line of code doesn't create anything (except probably a crash), and scanf doesn't allocate an array either.
    searches array for no. 1 to 10 and assigns value (j) to a[i]
    The loop that is in goes form 1 to 7 and doesn't assign j to anything.
    allocates memory for pointers, as random numbers can be entered by users
    There is only one pointer in your entire program! (no plural there) Thus there is only one thing to perhaps allocate memory for.

    Where are all these wrong ideas of how things work coming from? You really need to read through what the things you use actually do. If you don't actually know for certain how some of the stuff you're using works, then you simply cannot program using them. In computer programming you absolutely need to understand what you're doing. "Do, or do not; there is no try".
    Last edited by iMalc; 11-11-2009 at 01:36 AM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with file handling (pointers)
    By hmk in forum C Programming
    Replies: 5
    Last Post: 09-19-2008, 10:03 AM
  2. Problem with pointers
    By kotoko in forum C Programming
    Replies: 3
    Last Post: 06-12-2008, 05:17 AM
  3. A problem with pointers
    By vsla in forum C Programming
    Replies: 2
    Last Post: 10-10-2007, 04:14 AM
  4. Returning pointer to array of pointers problem
    By jimzy in forum C Programming
    Replies: 15
    Last Post: 11-11-2006, 06:38 AM
  5. Problem writing swap using pointers
    By joshdick in forum C++ Programming
    Replies: 1
    Last Post: 02-29-2004, 10:06 PM