GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 23-Sep-2004, 18:46
agentxx04 agentxx04 is offline
New Member
 
Join Date: Sep 2004
Posts: 12
agentxx04 is on a distinguished road
Exclamation

Need help with my programs, please help.


Hi. I recently sent my assignment to my professor. He sent it back to me saying that there was improper line spacing & that my program gave the wrong answer for square of average. This is my 1st time writing a program, so I would the last person to find any errors in my program. If anyone could kindly look at what I have so far & tell me what's wrong with my program, I would greatly appreciate it.

Thank You Very Much.



ASSIGNMENT 1

Write a program that prints the average and the square of the average of two numbers entered by the user. Declare all variables to be float.



Here's My Program:

CPP / C++ / C Code:
#include <stdio.h>

/*This program is designed to find the average of two numbers the
user enters and the average of those numbers.*/

int main()
{
	 float numbera;

	 float numberb;

	 float average;

	 float square;

	 printf("This program will find the average of two numbers. \n");
	 printf("Enter a number.");
	 scanf("%f",&numbera);
	 printf("Enter another number.");
	 scanf("%f",&numberb);
	 /*The average of the numbers is determined here.*/
	 average= (numbera + numberb)/2;
	 printf("The average of those two numbers %f\n", average);
	 /*The square of the average is determined here.*/
	 square= ((numbera + numberb) * (numbera + numberb));
	 printf("The square of the average is %f\n", square);

	 return(0);
}

/*End of Program*/



ASSIGNMENT 2

Write a program which asks the user to enter three numbers from the keyboard, and determines the smallest number, the largest number and (if the sum of all the numbers is not negative) the square root of the sum of all the numbers. If the sum is negative, the program informs the user of that fact.



The program must work for cases in which all data are negative, all data are positive, all data are the same, or the data are mixed negative and positive values.


Here's My 2nd Program

CPP / C++ / C Code:
#include <stdio.h>
#include <math.h>

/*This program is designed to find the smallest of 3 numbers, the largest,

the average, & the square root of the average.*/

int main()

{

        float a;  /*a represents 1st number input.*/
        
        float b;  /*b represents 2nd number input.*/

        float c;  /*c represents 3rd numer input.*/

        float avg; /*avg represents the average of those inputs.*/

        printf("Please Enter a number. ");

        scanf("%f",&a);

        printf("Enter another number. ");

        scanf("%f",&b);

        printf("Enter another number. ");

        scanf("%f",&c);

       /*This section of the program determines the smallest of the three inputs.*/

        if((a<b) && (a<c))
	 {

        printf("%f is the smallest number.\n", a);

	 }

        if((b<a) && (b<c))
        {
        printf("%f is the smallest number.\n", b);
        }

        else if((c<a) && (c<b))

        {

        printf("%f is the smallest number.\n", c);

        }

       /*This section of the program determines the largest of the three inputs.*/

     if((a>b) && (a>c))

     {

      printf("%f is the largest number.\n", a);

      }

      else if((b>a) && (b>c))

      {

      printf("%f is the largest number.\n", b);

      }

      else if((c>a) && (c>b))

      {

      printf("%f is the largest number.\n", c);

      }

       /*This section is in the event the sum of all three numbers are negative.*/

      if((a+b+c)<0)

      {

      printf("The sum is negative.\n");

      printf("You can't find the square root of a negative number!\n");

      }

      /*This section of the program determines the average of the three inputs.*/

      avg= (a+b+c)/3;

      printf("The average of the three numbers is %f\n", avg);

      printf("The square root of the average is %f\n", sqrt(a+b+c));


      return(0);
}

/*End of Program*/
Last edited by JdS : 24-Sep-2004 at 07:16. Reason: Please insert your example C/C++ codes between [c] and [/c] tags
  #2  
Old 23-Sep-2004, 19:02
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by agentxx04
Hi. I recently sent my assignment to my professor. He sent it back to me saying that there was improper line spacing & that my program gave the wrong answer for square of average. This is my 1st time writing a program, so I would the last person to find any errors in my program. If anyone could kindly look at what I have so far & tell me what's wrong with my program, I would greatly appreciate it.

Thank You Very Much.



ASSIGNMENT 1

Write a program that prints the average and the square of the average of two numbers entered by the user. Declare all variables to be float.



Here's My Program:
CPP / C++ / C Code:

     ...
	 average= (numbera + numberb)/2;
	 printf("The average of those two numbers %f\n", average);
	 /*The square of the average is determined here.*/
	 square= ((numbera + numberb) * (numbera + numberb));
	 printf("The square of the average is %f\n", square);
     ...


ASSIGNMENT 2

Write a program which asks the user to enter three numbers from the keyboard, and determines the smallest number, the largest number and (if the sum of all the numbers is not negative) the square root of the sum of all the numbers. If the sum is negative, the program informs the user of that fact.



The program must work for cases in which all data are negative, all data are positive, all data are the same, or the data are mixed negative and positive values.


Here's My 2nd Program


CPP / C++ / C Code:

     ...

      /*This section of the program determines the average of the three inputs.*/

      avg= (a+b+c)/3;

      printf("The average of the three numbers is %f\n", avg);

      printf("The square root of the average is %f\n", sqrt(a+b+c));

      ...



Are you trying too hard?

Problem 1:
The square of the average is average * average

Problem 2: did he ask for average and square root of average? Or did he ask for the sum and the square root of the sum?

Don't know what he means by improper line spacing. Maybe he has style requirements (like no double-spacing or something).

Regards,

Dave
 
 

Recent GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
help in c programs kitin21 C Programming Language 4 08-Sep-2004 08:38
Using datafeeds for affiliate programs Div MySQL / PHP Forum 23 05-Mar-2004 23:37
Programs that need expert eye C++ Forum 3 16-Jan-2004 06:02
premade forums and other premade php programs invaderz MySQL / PHP Forum 1 06-Jul-2003 03:10

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 02:28.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.