# array and loop help

• 03-26-2009
hockey1
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]);                         } }```
• 03-26-2009
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
• 03-26-2009
caroundw5h
Quote:

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; }```
• 03-26-2009
BEN10
Quote:

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; }```