Question

Identify the errors in this C program (struct - calculating volume, area of cylinder). Explain. #include...

Identify the errors in this C program (struct - calculating volume, area of cylinder). Explain.

#include <stdio.h>
#define PI 3.14

typedef struct cylinder{
   float r,h;
   float areacircle=2*PI*r;
   float areacylinder;
   float volumecylinder;
   areacylinder=2*PI*r*h+2*PI*r*r;
   volumecylinder=PI*r*r*h;
};

int main()
{
   float start,numcyls;
   printf("How many cylinders do you want? Enter a positive integer.\n");
   scanf("%f",&numcyls);
   if(numcyls<1||ceilf(numcyls)!=numcyls)
   while()
   {printf("The number you have entered is not a positive integer. Please try again.\n");
       printf("Remember, you have been asked to enter the number of cylinders you want.\n");
       printf("Hence, you can only enter a positive integer.\n");
       scanf("%f",&numcyls);
       if(numcyls>1&&ceilf(numcyls)==numcyls) break;
   }
   else
   {for(start=1;start<=numcyls;start++)
       {printf("Enter radius of cylinder\n");
           scanf("%f",&r);
           printf("Enter length of axis of cylinder\n");
           scanf("%f",&h);
           printf("The area of the circular cross-section is: \n",areacircle);
           printf("The surface area of the cylinder is: \n",areacylinder);
           printf("The volume of the circular cross-section is: \n",volumecylinder);
           return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Most of the erros are commented at their place, some main errors are trying to define area and volume inside struct which is //not possible in c and not creating struct and trying to call

//Missing math.h for ceil function, instead of ceilf, write ceil
#include <stdio.h>
#include<math.h>
#define PI 3.14

typedef struct cylinder{
float r,h;
}cylinder;

float areacircle(float r){
//area of circle is Pi*r*r instead of 2*Pi*r
return PI*r*r;
}

float areacylinder(float r,float h){
return 2*PI*r*h+2*PI*r*r;
}

float volumecylinder(float r,float h){
return PI*r*r*h;
}

int main()
{
float start,numcyls;
printf("How many cylinders do you want? Enter a positive integer.\n");
scanf("%f",&numcyls);
if(numcyls<1||ceil(numcyls)!=numcyls)
{
while(1)
{
printf("The number you have entered is not a positive integer. Please try again.\n");
printf("Remember, you have been asked to enter the number of cylinders you want.\n");
printf("Hence, you can only enter a positive integer.\n");
scanf("%f",&numcyls);
if(numcyls>1&&ceil(numcyls)==numcyls) break;
}
}
//change else part to checking for number of cylinders again
if(numcyls>1&&ceil(numcyls)==numcyls)
{
for(start=1;start<=numcyls;start++)
{
cylinder cylinde;
printf("Enter radius of cylinder %0.0f\n",start);
scanf("%f",&cylinde.r);
printf("Enter length of axis of cylinder %.0f\n",start);
scanf("%f",&cylinde.h);
printf("The area of the circular cross-section is: %f \n",areacircle(cylinde.r));
printf("The surface area of the cylinder is: %f \n",areacylinder(cylinde.r,cylinde.h));
printf("The volume of the circular cross-section is:%f \n",volumecylinder(cylinde.r,cylinde.h));
printf("--------------------------------------------------------------------------------------");
}
}
//placing return 0 outside loop so that it wont stop program
return 0;
}//missing paranthesis

Add a comment
Know the answer?
Add Answer to:
Identify the errors in this C program (struct - calculating volume, area of cylinder). Explain. #include...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I wrote a program which computes the area and perimeter of a square, circle, or rectangle. As you will see in my main function, there is a for loop in which the user is supposed to be able repeat the...

    I wrote a program which computes the area and perimeter of a square, circle, or rectangle. As you will see in my main function, there is a for loop in which the user is supposed to be able repeat the program until they enter "q" to quit. However, my program runs through one time, the prompt appears again, but then it terminates before the user is allowed to respond to the prompt again. I'm not able to debug why this...

  • i need help converting this code to java please #include<stdio.h> typedef struct{ int pid,at,bt,ct,tat,wt,f; }process; int...

    i need help converting this code to java please #include<stdio.h> typedef struct{ int pid,at,bt,ct,tat,wt,f; }process; int main() { int n,i,j,st=0,c,tot=0,pno=0,swi=0; float atat=0,awt=0; printf("enter no of processes : "); scanf("%d",&n); process a[n],temp; for (i=0;i<n;i++){ a[i].pid=i+1; a[i].f=0; printf("enter at : "); scanf("%d",&a[i].at); printf("enter the bt : "); scanf("%d",&a[i].bt); printf("--------------------------- "); } while(1){ int min=999,c=n; if (tot==n) break; for (i=0;i<n;i++){ if ((a[i].at<=st)&&(a[i].f==0)&&(a[i].at<min)){ min=a[i].at; c=i; } } if(pno!=a[c].pid) swi++; if (c==n) st++; else{ a[c].ct=st+a[c].bt; st=st+a[c].bt; a[c].tat=a[c].ct-a[c].at; atat=atat+a[c].tat; a[c].wt=a[c].tat-a[c].bt; awt=awt+a[c].wt; a[c].f=1; tot++; } } printf("...

  • So I have a question in regards to my program. I'm learning to program in C...

    So I have a question in regards to my program. I'm learning to program in C and I was curious about the use of functions. We don't get into them in this class but I wanted to see how they work. Here is my program and I would like to create a function for each of my menu options. I created a function to display and read the menu and the option that is entered, but I would like to...

  • NEED CODE HELPS WITH C PROGRAMMING. ISSUES EXPLAINED IN BOLD. COMPARING TWO LETTERS AND CALLING A...

    NEED CODE HELPS WITH C PROGRAMMING. ISSUES EXPLAINED IN BOLD. COMPARING TWO LETTERS AND CALLING A FUNCTION.   Problem are explained in bold #include <stdio.h> #include <string.h> #include <stdlib.h> #define pi 3.1415 void Area (double n); void VolSphere (double n); void Circumference (double n); void AllCalc (); // i dont know if the declaration of the function is correct AllCalc = AllCalculations //function main begins program execution int main (void) { puts("-Calculating Circle Circumference, Circle Area or Sphere Volume-\n"); void (*f[4])...

  • Please do this in C language. Thank you 1. Write the following functions that compute the...

    Please do this in C language. Thank you 1. Write the following functions that compute the volume and surface of a sphere with radius r; a cylinder with circular base with radius r and height h; and a cone with circular base with radius r and height h. Place these functions in appropriate class. Define n (PI) as a constant variable equals to 3.14 then use it in the functions. float sphereVolume(float r) float sphereSurface(float r) float cylinderVolume(float r, float...

  • Write a complete C program. Define a structure type element_t to represent one element from the...

    Write a complete C program. Define a structure type element_t to represent one element from the periodictable of elements. Components should include the atomic number (aninteger); the name, chemical symbol, and class (strings); a numeric field forthe atomic weight; and a seven-element array of integers for the number ofelectrons in each shell. The following are the components of an element_t structure for sodium.11 Sodium Na alkali_metal 22.9898 2 8 1 0 0 0 0 Have the user enter the data...

  • Write a C++ program to compute the total volume of N cones, where N is a...

    Write a C++ program to compute the total volume of N cones, where N is a number entered by the user. Repeat the request for N until the user enters a positive number. For each cone, ask for the radius and height, compute the volume, and add that volume to the total. Each time you ask the user to enter either the height or the radius, if the user does not enter a positive number, display an error message and...

  • C Programming I have this cost estimation program, but I want to add to it more...

    C Programming I have this cost estimation program, but I want to add to it more functions to be more accurate and more useful. I want to add to the program an array and a loop that can help me with the materials that can be use to the building, as ceramic, wood and concrete and extra functions. ########## #include <stdio.h> int main(void) { float wid,len,yrs; int metersq = 2000; int floors,rooms,win,doors,restrooms,umet; char floortype[20]; char ti[20]; int woodfloor = 25;...

  • Fix the errors in C code #include <stdio.h> #include <stdlib.h> void insertAt(int *, int); void Delete(int...

    Fix the errors in C code #include <stdio.h> #include <stdlib.h> void insertAt(int *, int); void Delete(int *); void replaceAt(int *, int, int); int isEmpty(int *, int); int isFull(int *, int); void removeAt(int *, int); void printList(int *, int); int main() { int *a; int arraySize=0,l=0,loc=0; int choice; while(1) { printf("\n Main Menu"); printf("\n 1.Create list\n 2.Insert element at particular position\n 3.Delete list.\n4. Remove an element at given position \n 5.Replace an element at given position\n 6. Check the size of...

  • Need help for C program. Thx #include <stdio.h> #include <string.h> #include <ctype.h> // READ BEFORE YOU...

    Need help for C program. Thx #include <stdio.h> #include <string.h> #include <ctype.h> // READ BEFORE YOU START: // This homework is built on homework 06. The given program is an updated version of hw06 solution. It begins by displaying a menu to the user // with the add() function from the last homework, as well as some new options: add an actor/actress to a movie, display a list of movies for // an actor/actress, delete all movies, display all movies,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT