Malloc and array problem

This is a discussion on Malloc and array problem within the C Programming forums, part of the General Programming Boards category; Here is the general assignment: "Write a C program that calculates the average price of a company’s stock over a ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    63

    Question Malloc and array problem

    Here is the general assignment: "Write a C program that calculates the average price of a company’s stock over a specified period of time. Your program should ask the user the number of prices that will be entered and then dynamically allocate the correct amount of memory to store the prices. Pass this allocated memory into functions that calculate the average price."

    Here's what I have so far

    Code:
    /*
    
    My name is Jack Trocinski
    
    */
    
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    	double i; // number of stocks
    	double *ptr; // memory allocation
    	
    	printf("Please enter the amount of stocks prices you wish to have calculated: ");
    	scanf("%d", &i);
    	
    	*ptr = malloc( i * sizeof(*ptr) );
    	
    	system("pause");
    	return 0;
    }
    I'm having problems just allocating the memory, what am I doing wrong with the malloc?? Then later I know I will probably need a function with an array to where the stock prices are stored and then calculated.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    ptr =, not *ptr =

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    63
    Oh nice, thanks. I will also make i an int value and then typecast it.

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    63
    Ok, now I want to have an array with i slots so I tried doing something like this but it's getting errors.
    Code:
    /*
    
    My name is Jack Trocinski
    
    */
    
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    	int j; // number of stocks
    	double *ptr; // memory allocation
    	int i; // used for loop
    	
    	printf("Please enter the amount of stocks prices you wish to have calculated: ");
    	scanf("%d", &j);
    	
    	double array[i];
    	
    	ptr = (double*)malloc( j * sizeof(*ptr) );
    	
    	system("pause");
    	return 0;
    }
    How can I get the user to specify the amount of slots the array has?

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    How can I get the user to specify the amount of slots the array has?
    You already did when you asked how many stocks...
    In my experience giving a user a choice like that is begging them to get it wrong.

    How many stocks? 10,000
    How many array slots? 6

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    63
    Yes exactly, so what is wrong with the code I posted. I would think since the user enters i number of stocks that if i create an array right after scanf called "double array[i]" it would make that many slots for me but I get errors.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by yacek View Post
    Yes exactly, so what is wrong with the code I posted. I would think since the user enters i number of stocks that if i create an array right after scanf called "double array[i]" it would make that many slots for me but I get errors.
    That's going to depend on the compiler you're using. The ability to make an array on the fly without using malloc is a c-99 feature that older compilers won't support.

    Beside that you asked for J ... not I... and you created an array ptr with J elements using malloc that can hold your stocks... you can access it as ptr[n] ... so why do you need 2 arrays?
    Last edited by CommonTater; 12-08-2010 at 10:23 PM.

  8. #8
    Registered User
    Join Date
    Oct 2010
    Posts
    63
    yes you're right I used J, got mixed up there. So by using malloc I've already created an array? I didn't realize that. Ok, I'm going to play with this some more I'm sure I'll have some more questions but I want to see how far I can get with it first. Thanks.

  9. #9
    Registered User \007's Avatar
    Join Date
    Dec 2010
    Posts
    179
    You're begging for a challenging user.

    Q:"How many stocks?"
    A: -10
    *CRASH*

    Make sure to check for proper input.

  10. #10
    Registered User
    Join Date
    Oct 2010
    Posts
    63
    Oh nice, didn't think of that. Thanks. Probably can fix that with an if else statement.

  11. #11
    Registered User
    Join Date
    Oct 2010
    Posts
    63
    Alright finally got the average to work out, now any suggestions how to find the lowest and highest values entered since the way I tried isn't really working. New code:

    Code:
    /*
    
    My name is Jack Trocinski
    
    */
    
    #include <stdio.h>
    #include <stdlib.h>
    
    double average(double *ptr, int j);
    
    int main()
    {
    	int j; // number of stocks
    	double *ptr; // memory allocation
    	int i; // used for loop
    	double min; // lowest price
    	double max; // highest price
    	
    	printf("Please enter the amount of stocks prices you wish to have calculated: ");
    	scanf("%d", &j);
    	
    	if (j > 0) {
    		ptr = (double*)malloc( j * sizeof(*ptr) );
    		for ( i = 0; i < j; ++i) {
    			printf("Input your stock price and press enter: ");
    			scanf("%lf", &ptr[i]);
    			
    			if (min < ptr[i]) min = ptr[i];
    			if (max > ptr[i]) max = ptr[i];
    		}
    		
    		printf("The average price entered was %f \n ", (average(ptr, j)) );
    		printf("The lowest price entered was: %f \n", min);
    		printf("The highest price entered was: %f \n", max);
    		
    		free( ptr );
    		
    	}
    	
    	else printf("Sorry, you have entered an invalid number.\n");
    	
    	system("pause");
    	return 0;
    }
    
    double average(double *ptr, int j)
    {
    	int i; // for loop
    	double sum = 0; // sum of array
    	double avg; // avg of array
    	
    	for ( i = 0; i < j; ++i) {
    		sum = sum + ptr[i];
    	}
    	
    	avg = ( sum / ((double)j) );
    	return avg;
    }

  12. #12
    Registered User
    Join Date
    Oct 2010
    Posts
    63
    I have the max price working now using this code instead in the main for loop, but the min keeps showing me 0.000000 as the lowest price even though i never enter a zero value to the array... suggestions?

    Code:
    	if (ptr[i] < min) min = ptr[i];
    		if (ptr[i] > max) max = ptr[i];

  13. #13
    Registered User
    Join Date
    May 2010
    Posts
    2,918
    What does isn't working mean?

    You might want to initialize the min, max variables before you use them.

    Jim

  14. #14
    Registered User
    Join Date
    Oct 2010
    Posts
    63
    Quote Originally Posted by jimblumberg View Post
    What does isn't working mean?


    Jim
    The lowest price entered is printing out a 0.000000 every time I run it even though I never enter a 0 price value into the array. The highest price entered works fine though.

  15. #15
    Registered User
    Join Date
    May 2010
    Posts
    2,918
    You might want to initialize the min, max variables before you use them. So the have a valid value.

    Jim

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc problem
    By abc_killer in forum C Programming
    Replies: 9
    Last Post: 11-15-2010, 02:12 PM
  2. Replies: 7
    Last Post: 05-19-2010, 03:12 AM
  3. dynamic array and structure problem
    By bluetxxth in forum C Programming
    Replies: 3
    Last Post: 04-13-2010, 07:56 AM
  4. Problem with malloc() and sorting words from text file
    By goron350 in forum C Programming
    Replies: 11
    Last Post: 11-30-2004, 10:01 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM

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