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 02-Mar-2004, 19:31
soulfly soulfly is offline
New Member
 
Join Date: Mar 2004
Posts: 8
soulfly is on a distinguished road
Question

Please Help, problems writing newbie c program


Please Help, What is wrong with this program ???

CPP / C++ / C Code:
/*  Group Project pg 299, # 58
    written by:  
	3-3-04 

This program reads a list of integers from the keyboard and creates the following information:
find and print the sum and the average of the integers, find and print the largest and smallest
integer, print a boolean true or false if some of them are less than 20, print a boolean 
true or false if all of them are betrween 10 and 90.*/



#include <stdio.h>

/* Prototype Declarations*/

int totalNum (int totalintegers);
int findSum (int sum);
float findAverage (float avg);
int findLarge (int largest);
int find;
int findBool (int boolone , int booltwo);


int main (void)

{

/*Local Definitions*/

int num = 0;
int sum = 0;
float avg = 0.0;
int largest = 0;
int smallest = 0;
int boolone;
int booltwo;
int boolean;
int totalintegers = 0;

/*Statememts*/

printf(" Please enter numbers: <return> : Enter 99999 to stop");
scanf("%d",&num);

do

{
	totalintegers = totalNum (totalintegers);
	sum = findSum (sum);
    avg = findAverage (avg);
	largest = findLarge (largest);
	boolean = findBool (boolone,booltwo);
}

while (num!=99999);


printf(" The number of integers is:              %d\n",totalintegers);
printf(" The sum of the integers is:            %d\n",sum);
printf(" The average of the intergers is:        %d\n",avg);
printf(" The smallest integer is:                %d\n",smallest);
printf(" The largest integer is:                 %d\n",largest);

if (boolone !=0)
     
     printf(" True, some of the integers are less than twenty\n");

else

     printf(" False, none of the integers are less than twenty\n");

if (booltwo!=0)
  
    printf(" True, all of the integers are between 10 and 90\n");

else

    printf(" False, none of the integers are between 10 and 90\n");

return 0;

}




int totalNum (int totalintegers)



{
	totalintegers ++;

return (totalintegers);

}



int findSum (int sum , int num)

int num;
int sum;

{
	sum = num + num;


return (sum);

}



float findAverage (float avg)

float avg;
int sum;
int totalintegers;

{

	avg = sum / toalintegers;

return (avg);
}



int findLargest (int largest int smallest)

int num;
int largest;
int smallest;

{

	if (num > num)

		largest = num;

	else

		smallest = num;

return (largest smallest);

}

int findBool (int boolone int booltwo)

int num;
int boolone;
int booltwo;

{

	if (num < 20 )

		boolone = 1; 

	else

		boolone = 0;

	if (num > 10 && num < 90)

		booltwo = 1;

	else

		booltwo = 0;


return (boolone booltwo);

}
Last edited by dsmith : 02-Mar-2004 at 19:41. Reason: Added C-syntax highlighting
  #2  
Old 02-Mar-2004, 19:57
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hello Soulfly. I am assuming that this program won't compile. It helps when you post if you can give us a hint of what is not working

It appears that you are familiar with Pascal. In C, function calls are a bit different. First of all, you do not give the type of the parameters again. Also, C can only return a single value. If you want to return a parameter, you must pass it as an address and receive it as a pointer. For example, this function:

CPP / C++ / C Code:
int findBool (int boolone int booltwo)

int num;
int boolone;
int booltwo;

{

	if (num < 20 )

		boolone = 1; 

	else

		boolone = 0;

	if (num > 10 && num < 90)

		booltwo = 1;

	else

		booltwo = 0;


return (boolone booltwo);

}

Should look more like:
CPP / C++ / C Code:
void findBool (int num, int *boolone, int *booltwo)
{

	if (num < 20 )

		*boolone = 1; 

	else

		*boolone = 0;

	if (num > 10 && num < 90)

		*booltwo = 1;

	else

		*booltwo = 0;

}

This function would probably make more sense written as two functions, one that would return boolone and one that would return booltwo. For example:

CPP / C++ / C Code:
int findBool1(int num)
{
  int boolone;

  if (num < 20 )
     boolone = 1; 
  else
    boolone = 0;

  return(boolone);
}

Then to call it, use:
CPP / C++ / C Code:
boolone = findBool1(num);

All of your functions need to be changed to be similar to this.

HTH
  #3  
Old 02-Mar-2004, 20:06
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
Wow... okay, first of all, you need to separate your function parameters with commas. Second of all, once you declare a variable within a function header, it is local to that function and does not need to be declared again. You are declaring variables under your function headers, which will cause your program not to compile.
CPP / C++ / C Code:
float findAverage (float avg) // needs more parameters... sum, totalintegers

float avg;              // This stuff has got to go. Check out below to see why
int sum;
int totalintegers;

{
  avg = sum / toalintegers;
  return (avg);
}



int findLargest (int largest int smallest) // need a comma between parameters

int num;       // Don't declare variables in the space between the function
int largest;   // header and the body. the variables names are also already
int smallest; // declared (except for num) within the function header above

{
  if (num > num)
    largest = num;
  else
    smallest = num;

  return (largest smallest);
}

int findBool (int boolone int booltwo) // need a comma between parameters

int num;       // This is a no no in C. These are already defined in the function
int boolone;  // header and are local to the scope of the function, except
int booltwo;  // for num, which needs to be if you want to use it.

{
  if (num < 20 )
    boolone = 1; 
  else
    boolone = 0;
  if (num > 10 && num < 90)
    booltwo = 1; 
 else
    booltwo = 0;

   return (boolone booltwo);
}
  #4  
Old 02-Mar-2004, 20:15
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
Quote:
Originally Posted by dsmith
If you want to return a parameter, you must pass it as an address and receive it as a pointer.

Actually, in C++ you can simply receive the address and pass normally.
CPP / C++ / C Code:
void myfunction(int &somenumber); // the & operator gives you the address

int main()
{
  int somenumber = 5;
  
  myfunction(somenumber); //The function is allowed to change the value of
                          //the variable local to main(), because I received
  return 0;               //the address of the variable that I passed
}
  #5  
Old 02-Mar-2004, 21:52
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by aaroncohn
Actually, in C++ you can simply receive the address and pass normally.
Or more accurately:
CPP / C++ / C Code:
void myfunction(int *thisnumber)  // * represents a pointer being passed in
{
  *thisnumber = 10;  // return 10 into the value pointed at
}

int main()
{
  int somenumber = 5;
  myfunction(&somenumber);  // pass in the address (pointer) of the variable
  return 0;
}
  #6  
Old 02-Mar-2004, 22:11
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
Isn't that exactly what dsmith said? I was saying that you only need to use the reference operator in the function header, then anything you pass to it will be passed by address.
  #7  
Old 02-Mar-2004, 22:37
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
My mistake I forgot about the C++ difference. You are correct.
  #8  
Old 03-Mar-2004, 08:22
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Quote:
Originally Posted by aaroncohn
Actually, in C++ you can simply receive the address and pass normally.
CPP / C++ / C Code:
void myfunction(int &somenumber); // the & operator gives you the address

int main()
{
  int somenumber = 5;
  
  myfunction(somenumber); //The function is allowed to change the value of
                          //the variable local to main(), because I received
  return 0;               //the address of the variable that I passed
}

Okay, if there is one thing that I am, it is opinionated . And I just have to give my opinion on this. I am an old dinasour and I have never had a formal class in C++, but I do like it and I use some of its features and I am happy. But this is sick & wrong (IMHO), this does something totally different in C than in C++ apparently.

I don't like this, because if you are familiar with C++ code and are reviewing/maintaining C code and try to do this you will get very different results than you expected.

Once again, this is my opinion and I don't want to start a holy war. But if I was learning C++, I would avoid this type of function calling like the plague.

I am open to hearing differing opinions, but keep the hits above the belt please.
  #9  
Old 03-Mar-2004, 16:59
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
I agree also. It hides the realities of the parameters. But in their defense, it does make the coding easier on the calling side. The compiler does all the thinking. I wonder if Billy had a lot of input
  #10  
Old 03-Mar-2004, 19:04
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
I'm doing my Mouse-on-an-Island-with-Bridges-on-It program using the C method for portability. I haven't written anything large enough to cause problems while using the reference operator in function headers, so I'll probably just start using the C way instead.
 
 

Recent GIDBlogToyota - 2008 August Promotion by Nihal

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
Chaintech Geforce 5600 FX problems bartster74 Computer Hardware Forum 8 04-May-2004 13:16
[Tutorial] Standard I/O aaroncohn C Programming Language 20 27-Feb-2004 21:07
compiling a program within another (C++) Siphiro C++ Forum 5 06-Feb-2004 15:35
error during program rjd72285 C++ Forum 0 11-Nov-2003 18:49
one program access another? dgoulston C++ Forum 1 07-Oct-2003 11:26

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

All times are GMT -6. The time now is 20:11.


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