array and loop help

This is a discussion on array and loop help within the C Programming forums, part of the General Programming Boards category; hi guys. been workin on this program for a while now and i cant seem to get it to output ...

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

    array and loop help

    hi guys. been workin on this program for a while now and i cant seem to get it to output correctly. i know somethings wrong with my loop and my array...just need some direction and what not.

    the program asks the user how many circles to calculate (between 1 and 10). then it takes that imput and begins to ask for a radi for each. after that, the program puts the imputed radi through a couple equations to calculate circumference and area and prints the results.

    problem is that if the user enters a number over 10, it outputs the error but then runs directly into prompting for radi, it shouldnt do this. instead i need it to ask to imput a number of circles again.
    also, its not storing the radi into an array of "circleno" (the number the user entered for how many circles to calculate) length. it takes the last radi entered and runs it through the loop to calculate the circumference and area. then prints the same answers for all circles.

    i appreciate the help.

    Code:
    #include <stdio.h>
    #define PI 3.14159265
    #define CONSTANT 10
    int main (void)
    {
    	int circleno;
    	int j;
    	int i;
    	
    	printf("Please enter the number of circles (max = 10):\n");
    	scanf("%i", &circleno);
    	
    	float radius[circleno];
    	float circumference[circleno];
    	float area[circleno];
    	
    	if ( circleno > CONSTANT )
    		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[circleno]);
    
    				circumference[circleno] = 2*PI*radius[circleno];
    				area[4] = PI*radius[circleno]*radius[circleno];
    			}
    			printf("radius     circumference     area     \n");
    			printf("======================================\n");
    			for ( i = 1; i <= circleno; ++i ) {
    				printf("%f         %f                %f       \n", radius[circleno], circumference[circleno], area[circleno]);
    			}
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
    #include <stdio.h>
    #define PI 3.14159265
    #define CONSTANT 10
    int main (void)
    {
    	int circleno;
    	int j;
    	int i;
    	
          do  {
    	  printf("Please enter the number of circles (max = 10):\n");
    	  scanf(" %i", &circleno);
          }while(circleno < 1 || circleno > 10);	
    
    	float radius[circleno];
    	float circumference[circleno];
    	float area[circleno];
    	
    	for ( j = 1; j <= circleno; ++j ) {
       	   printf("Enter the radius for circle #%i:\n", j);
    	   scanf("%f", &radius[circleno]);
       	   circumference[circleno] = 2*PI*radius[circleno];
    	   area[4] = PI*radius[circleno]*radius[circleno];
    	}
    	printf("radius     circumference     area     \n");
    	printf("======================================\n");
    	for ( i = 1; i <= circleno; ++i ) {
    	   printf("%f         %f                %f       \n", radius[circleno], circumference[circleno],
              area[circleno]);
           }
    }
    Try something like this

  3. #3
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    problem is that if the user enters a number over 10, it outputs the error but then runs directly into prompting for radi, it shouldnt do this. instead i need it to ask to imput a number of circles again.
    also, its not storing the radi into an array of "circleno" (the number the user entered for how many circles to calculate) length. it takes the last radi entered and runs it through the loop to calculate the circumference and area. then prints the same answers for all circles.
    I cleaned up your code a bit to use some precison specifiers and a simple while loop. When in doubt about why your code is acting up, a simple print statement can go a long way. compile and run it and see what value circleno has.
    Code:
    #include <stdio.h>
    #define PI 3.14159265
    #define CONSTANT 10
    int main (void)
    {
    	int circleno,i, j;
    	/*circleno < CONSTANT initially so while block is evaluated first*/
      while ( circleno > CONSTANT){
            printf("Please enter the number of circles (max = 10):");
           	scanf("%i", &circleno);
      }
             
    	
    	float radius[circleno], circumference[circleno], area[circleno];
    		
    	for ( j = 0; j < circleno; j++ ){
          printf("Enter the radius for circle #%i:\n", j);
          scanf("%f", &radius[circleno]);
          
        printf("circleno is: %d\n", circleno);
         
          
          circumference[circleno] = 2*PI*radius[circleno];
          
          area[circleno] = PI* (radius[circleno] *radius[circleno]);
    	}
      
      printf("radius     circumference     area     \n");
      printf("======================================\n");
      
      /*notice anything?? */
      printf("circle no: %d\n", circleno);
      
      for ( i = 0; i < circleno; i++ ){
          printf("%.2f  %15.2f  %15.2f\n", radius[circleno], circumference[circleno], 
          area[circleno]);
    	}
    
    return 0;
    }

  4. #4
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    problem is that if the user enters a number over 10, it outputs the error but then runs directly into prompting for radi, it shouldnt do this. instead i need it to ask to imput a number of circles again.
    also, its not storing the radi into an array of "circleno" (the number the user entered for how many circles to calculate) length. it takes the last radi entered and runs it through the loop to calculate the circumference and area. then prints the same answers for all circles.
    i've modified ur code and it works.

    Code:
    #include <stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    #define PI 3.14159265
    #define CONSTANT 10
    int main (void)
    {
    	int circleno;
    	int j;
    	int i;
    	
    	printf("Please enter the number of circles (max = 10):\n");
    	scanf("%d", &circleno);
    	
    	float radius[10];//while declaring array it should have constant size
    	float circumference[10];
    	float area[10];
    	
    	if ( circleno > CONSTANT ){
    		printf("Error -> %d circles is to many. Please try again:\n", circleno );
    		exit(0);}
    		else
    			for ( j = 1; j <= circleno; ++j ) {
    				printf("Enter the radius for circle #%d:\n", j);
    				scanf("%f", &radius[j]);//radius[circleno] is the radius of last circle which gets stored each time for every circle,thus radius[j] is used
    				circumference[j] = 2*PI*radius[j];//same reason as above				
    area[j] = PI*radius[j]*radius[j];//same reason as above
    			}
    			printf("radius     circumference     area     \n");
    			printf("======================================\n");
    			for ( i = 1; i <= circleno; ++i ) {
    				printf("%f         %f                %f       \n", radius[i], circumference[i], area[i]);
    			}
    			getch();
    			return 0;
    }
    Last edited by BEN10; 03-26-2009 at 09:56 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. For loop through a 2D array - just for select rows
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 04-17-2009, 08:34 AM
  2. Need loop to copy chunks from byte array
    By jjamjatra in forum C# Programming
    Replies: 2
    Last Post: 03-05-2009, 04:42 AM
  3. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  4. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 11:09 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM

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