Question

Below is the code for two programs in C language, I was trying to make proper...

Below is the code for two programs in C language, I was trying to make proper comments within the code so that someone can understand it. Please review the comments within the code and let me know if it makes good sense. Let me know if I need to make any changes to the comments! Thank you.

PROGRAM 1:

/* program calculates harmonic mean for 10 numbers entered by the user */

/* if illegal value (x<0) is entered, the user is asked to re-enter a valid number */

#include "stdafx.h"

#include <math.h>

#include <stdio.h>

      int main()

      {

            int n = 10;

            float x, prod = 1, gm;          /* declaring variables, x is for taking input */

            for (int i = 1; i <= n; i++)      /* i = counter starting at 1, loop runs              10 times by increment of 1 */

            {

                  printf("enter a number\n");    /* asking user to enter number */

                  scanf_s("%f", &x); /* number entered by user */

                  if (x < 0) /* (x<0) for invalid entry */

                  {

                  printf("you entered an invalid number\n");

      i--;  /*i = i – 1, the count will be reduced to accommodate a new   entry by the user*/

                  continue;

                  /* using continue to ask the user to re-enter a valid  number */

      /* continue statement forces next iteration of the loop and skips   any remaining statements in the loop */

                  }

                  prod *= x; /* prod = prod * x */

            }

gm = pow(prod, 10 / n); /* formula for geometric mean using previous statements */

            printf("the harmonic mean is %f:\n",gm); /* displaying result of geometric mean */

            return 0;

      }

PROGRAM 2:

/* program to find the average speed and the maximum height */

/* information is obtained from the data file */

#include "stdafx.h"

#include <math.h>

#include <stdio.h>

int main()

{

      int speed, N;

      FILE*t1;     /* FILE declaration defines file pointer */

      float height, max, sum = 0, avg_speed;

      t1 = fopen("input.txt", "r");/* fopen function used to obtain the information     needed to assign file pointer to the file  */

      fscanf(t1, "%d", &N); /* fscanf function is used to scan the file */

      for (int k = 1; k <= N; k++)

      {

            fscanf(t1, "%d %f", &speed, &height); /* fscanf function is used to scan               the file */

            if (k == 1)

                  max = height;

            if (height > max)

                  max = height;

            sum += speed;

      }

      avg_speed = sum / N;

      printf("the average is %f and max is %f", avg_speed, max); /* displaying result of      average speed and maximum value of height */

      return 0;

}

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

The comments are perfect. As most of comments are a single line comment, you can use the symbol // instead of /* ... */ before a comment.

Also, please comment why you used the header files. For example,

#include<math.h> // used for pow() function
#include<stdio.h> // used for input and output

These are just minor changes. Otherwise, everything is correct.

Add a comment
Know the answer?
Add Answer to:
Below is the code for two programs in C language, I was trying to make proper...
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
  • Please I need help in C language, I am trying to modify the code per the...

    Please I need help in C language, I am trying to modify the code per the below instructions, but I am getting errors. Can't fgure it out. The attempted code modification and data file is privided below, after the instructions. Thank you. Instructions:                  1.      First, add a function named printMsg that displays a message, a greeting, or an introduction to the program for the user. Add a statement that calls that function from the main function when your program starts....

  • How would I change the following C code to implement the following functions: CODE: #include <stdio.h>...

    How would I change the following C code to implement the following functions: CODE: #include <stdio.h> #include <stdlib.h> int main(int argc, char * argv[]) { if (argc != 2) printf("Invalid input!\n"); else { FILE * f = fopen (argv[1], "r"); if (f != NULL) { printf("File opened successfully.\n"); char line[256]; while (fgets(line, sizeof(line), f)) { printf("%s", line); } fclose(f); } else { printf("File cannot be opened!"); } } return 0; } QUESTION: Add a function that uses fscanf like this:...

  • Help with my code: The code is suppose to read a text file and when u...

    Help with my code: The code is suppose to read a text file and when u enter the winning lotto number it suppose to show the winner without the user typing in the other text file please help cause when i enter the number the code just end without displaying the other text file #include <stdio.h> #include <stdlib.h> typedef struct KnightsBallLottoPlayer{ char firstName[20]; char lastName[20]; int numbers[6]; }KBLottoPlayer; int main(){ //Declare Variables FILE *fp; int i,j,n,k; fp = fopen("KnightsBall.in","r"); //...

  • **how can I make my program break if 0 is entered by user as last number...

    **how can I make my program break if 0 is entered by user as last number not always 10 numbers output should be enter 10 numbers:1 2 0 numbers entered by user are: 1 2 Largest number: 2 **arrays can ONLY be used in the main function, other than the main function pointers should be used #include #define N 10 void max(int a[], int n, int *max); int main (void) { int a [N], i , big; printf("enter %d numbers:",N);...

  • Please help in C: with the following code, i am getting a random 127 printed in...

    Please help in C: with the following code, i am getting a random 127 printed in front of my reverse display of the array. The file i am pulling (data.txt) is: 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 when the reverse prints, it prints: 127 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 not sure why...please...

  • Convert C to C++ I need these 4 C file code convert to C++. Please Convert...

    Convert C to C++ I need these 4 C file code convert to C++. Please Convert it to C++ //////first C file: Wunzip.c #include int main(int argc, char* argv[]) { if(argc ==1){ printf("wunzip: file1 [file2 ...]\n"); return 1; } else{ for(int i =1; i< argc;i++){ int num=-1; int numout=-1; int c; int c1;    FILE* file = fopen(argv[i],"rb"); if(file == NULL){ printf("Cannot Open File\n"); return 1; } else{ while(numout != 0){    numout = fread(&num, sizeof(int), 1, file);    c...

  • Convert this code from C to C++ include <stdio.h> include <locale.h> void filtre (double x. double yll, double yol, int , int N) int Nn-10000 int main) setlocale (LC ALL,") double...

    Convert this code from C to C++ include <stdio.h> include <locale.h> void filtre (double x. double yll, double yol, int , int N) int Nn-10000 int main) setlocale (LC ALL,") double x(Nm) , y [ Nm],yo Nml®(0.0); int m; FILE dosyal-fopen("D:/veri/673.txtr FILE dosya2-fopen("D:/veri/data2.txt int i-0 while( feof (dosyal)) fscanf (dosyal, " ",xi sytil) fclose (dosyal) int Nij printf("Pencere Genişligi scanf() 3 filtre(x,y.Yo,m,N) for(int k-0keNkt+)fprintf (dosya2, 10.41 10.411 0.41fn",x[k],y[k].yo[k]) fclose(dosya2) void filtre (double x double y. double yoll, int m, int...

  • URGENT. Need help editing some C code. I have done most of the code it just...

    URGENT. Need help editing some C code. I have done most of the code it just needs the following added: takes an input file name from the command line; opens that file if possible; declares a C struct with three variables; these will have data types that correspond to the data types read from the file (i.e. string, int, float); declares an array of C structs (i.e. the same struct type declared in point c); reads a record from the...

  • This is done in c programming and i have the code for the programs that it wants at the bottom i ...

    This is done in c programming and i have the code for the programs that it wants at the bottom i jut dont know how to call the functions Program 2:Tip,Tax,Total int main(void) {    // Constant and Variable Declarations    double costTotal= 0;    double taxTotal = 0;    double totalBill = 0;    double tipPercent = 0;    // *** Your program goes here ***    printf("Enter amount of the bill: $");    scanf("%lf", &costTotal);    printf("\n");    // *** processing ***    taxTotal = 0.07 * costTotal;    totalBill...

  • -I need to write a program in C to store a list of names (the last...

    -I need to write a program in C to store a list of names (the last name first) and age in parallel arrays , and then later to sort them into alphabetical order, keeping the age with the correct names. - Could you please fix this program for me! #include <stdio.h> #include <stdlib.h> #include <string.h> void data_sort(char name[100],int age[100],int size){     int i = 0;     while (i < size){         int j = i+1;         while (j < size){...

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