C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-10-2009, 03:19 PM   #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...)

Quote:

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.
jugs is offline   Reply With Quote
Old 11-10-2009, 04:15 PM   #2
Registered User
 
C_ntua's Avatar
 
Join Date: Jun 2008
Posts: 1,134
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.
C_ntua is offline   Reply With Quote
Old 11-11-2009, 01:34 AM   #3
Algorithm Dissector
 
iMalc's Avatar
 
Join Date: Dec 2005
Location: New Zealand
Posts: 2,475
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:
Quote:
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.
Quote:
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.
Quote:
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".
__________________
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger

Last edited by iMalc; 11-11-2009 at 01:36 AM.
iMalc is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 07:51 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22