Thread: help with simple program

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    22

    help with simple program

    this program im writing should take radius imputs from the user after the user selects how many circles (between 1 and 10) they would like to imput radi for. after that, it should store those radi in an array, and calculate circumference and area for each radi and print the results.

    what i have so far compiles just fine however when i run the ./a file (im using cygwin) i get an error of the following:
    18 [main] a 2604 _cygtls::handle_exeptions: Error while dumping state (progably corrupted stack)
    Segmentation fault (core dumped)

    whats goin on with the stack? how can it just corrupt like that...is it a problem whithin my program or my compiler?

    Code:
    #include <stdio.h>
    #define PI 3.14159265
    int main (void)
    {
    	int circleno, i;
    	int j;
    	float radius[i];
    	float circumference[i];
    	float area[i];
    	circleno = i;
    	
    	printf("Please enter the number of circles (max = 10):\n");
    	scanf("%i", &circleno);
    	
    	if ( circleno > 10 )
    		printf("Error -> %i circles is to many. Please try again:\n", circleno );
    		else
    			for ( j = 1; j <= circleno; ++j ) {
    				printf("Enter the radius for circle #%i:\n", j);
    				scanf("%f", &radius[i]);
    			}
    		circumference[i] = 2*PI*radius[i];
    		area[i] = PI*radius[i]*radius[i];
    	
    	printf("radi = %f\n", radius[i]);
    	printf("circumferences = %f\n", circumference[i]);
    	printf("areas = %f\n", area[i]);
    }

  2. #2
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    Question: does the compiler know what i is? You can't have a variable in an array during compilation, at least not in this instance. Use a number instead. You want a maximum of 10 so it'd be wise to have 10 here. Instead of %i, you'd want %d, for the string. %d is for displaying integers.
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    	int circleno, i;
    	int j;
    	float radius[i];
    	float circumference[i];
    	float area[i];
    	circleno = i;
    Ehm, what is the value of i when you set up those arrays ?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    22
    well i = circleno and "circleno" is the number of circles the user imputs to enter radi for. should i move it down in the code under the first printf and scanf like this?

    Code:
    #include <stdio.h>
    #define PI 3.14159265
    int main (void)
    {
    	int circleno;
    	int j;
    	
    	printf("Please enter the number of circles (max = 10):\n");
    	scanf("%i", &circleno);
    	
    	float radius[i];
    	float circumference[i];
    	float area[i];
    	circleno = i;

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    22
    ok i figured it out. however there is now a NEW problem...

    heres what the code looks like, but its not calculating the circumference and area for each radi entered. only the last one...maybe a loop?
    Code:
    #include <stdio.h>
    #define PI 3.14159265
    int main (void)
    {
    	int circleno;
    	int j;
    	int i;
    	circleno = i;
    	
    	printf("Please enter the number of circles (max = 10):\n");
    	scanf("%i", &circleno);
    	
    	float radius[i];
    	float circumference[i];
    	float area[i];
    	
    	if ( circleno > 10 )
    		printf("Error -> %i circles is to many. Please try again:\n", circleno );
    		else
    			for ( j = 1; j <= circleno; ++j ) {
    				printf("Enter the radius for circle #%i:\n", j);
    				scanf("%f", &radius[i]);
    			}
    		circumference[i] = 2*PI*radius[i];
    		area[i] = PI*radius[i]*radius[i];
    	
    	printf("radi = %f\n", radius[i]);
    	printf("circumferences = %f\n", circumference[i]);
    	printf("areas = %f\n", area[i]);
    	
    }

  6. #6
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    You have the loop, but you don't have the area and circumference elements within it. You should easily see this as these elements are outside of your loop.
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Actually, I don't see how that code has fixed anything - it is still as broken [but maybe you got lucky and i is a different value now when you have rearranged the code?]

    Right, so you need a loop that BOTH reads the data and calculates whatever you are calculating (or two loops, one for reading the data, and one for calculating it).

    Also, you are reading in radius[i], when you are using j to loop around. i is still not set to anything.

    Further, you still make calculations even after you said "too many circles".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with a very simple program
    By htdefiant in forum C++ Programming
    Replies: 13
    Last Post: 08-14-2007, 01:27 PM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. [Help] Simple Array/Pointer Program
    By sandwater in forum C Programming
    Replies: 3
    Last Post: 03-30-2007, 02:42 PM
  4. simple silly program
    By verbity in forum C Programming
    Replies: 5
    Last Post: 12-19-2006, 06:06 PM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM