C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old October 23rd, 2005, 03:29 PM
dekoi dekoi is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2005
Posts: 12 dekoi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 27 m 52 sec
Reputation Power: 0
User defined functions

I am instructed to do the following:

Quote:
Add a user defined function which does the calculation involving the minor product code. It should be passed the minor part of the product_code and the base_price, and should return the (possibly) modified price.Add a second user defined function which does the calculation involving the major product code. It should be passed the major part of the product_code and the price calculated up to that point, and should return the (possibly) modified price.Add a third user defined function which does the calculation involving the shipping. It should be passed the "where" part of the product_code and the price calculated up to that point, and should return the final price.


However, I'm not sure what the assignment means. Firstly, is "passed" supposed to be "past"?

Also, what does it mean to "return a possibly modified price"? Can anyone give me an example of how to use functions?

Here is my original code (which by the way, isn't even running due to the problem i mention here
Code:
#include <stdio.h>

int main()
{

       int prod_num, number, counter=0, major_code=0, minor_code=0,
tax_code=0, where=0, total_number=0;
       double base_cost, minor_factor=0, shipping_costs=0, tax_amount=0,
final_unitprice=0,average_final, final_subtotal=0, final_price_product;

       while ( scanf("%d %lf %d", &prod_num, &base_cost, &number) != EOF)
       {
               total_number += number;
			   counter++;
               major_code = prod_num / 100000;
               minor_code = (prod_num % 100000) / 100;
               tax_code = (prod_num % 100) / 10;
               where = prod_num % 10;

               if(minor_code >= 0 && minor_code <= 399) {
                       minor_factor = 1.45;
               }

               else if ((minor_code>=400 && minor_code <=799) || (minor_code>=950
&& minor_code <=955)) {
                       minor_factor = 1.12;
               }

               else {
                       minor_factor = 0.89;
               }

               switch(where)
               {
                       case 0:         shipping_costs = 7.50;
                                               break;

                       case 1:         shipping_costs = 8.47;
                                               break;

                       case 2:         shipping_costs = 6.05;
                                               break;

                       case 3:         shipping_costs = 10.20;
                                               break;

                       default:        shipping_costs = 20.00;
                                               break;

               }

               if(tax_code == 1)

                       {

                       if(major_code>=0 && major_code<=250)
                               tax_amount = 0.12;

                       else if(major_code>=251 && major_code<=450)
                               tax_amount = 0.15;

                       else
                               tax_amount = 0.25;

                       }

               else if (tax_code == 0)
                       tax_amount = 0;

               final_unitprice = (base_cost * minor_factor + shipping_costs) * (1 + tax_amount);
               final_price_product = final_unitprice * number;
			   final_subtotal += final_price_product;
			   
               printf("---------------------------> PRODUCT [%d]<---------------------------\n", counter);
               printf("\n");
               printf("Product #: %d || Price/Unit: $%.2f || # of Products: %d || 
Total: $%.2f\n", prod_num, final_unitprice, number, final_price_product);
               printf("\n");
               printf("\n");

       }

average_final = final_subtotal / total_number;

		printf("\n");
		printf("The total price is: $%.2f\n", final_subtotal);
		printf("The average price per product is: $%.2f", average_final);
		printf("\n");
		
       return 0;
}


Reply With Quote
  #2  
Old October 23rd, 2005, 04:04 PM
Geo.Garnett's Avatar
Geo.Garnett Geo.Garnett is offline
Registered Loser
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Location: Retardation Nation...
Posts: 347 Geo.Garnett User rank is Private First Class (20 - 50 Reputation Level)Geo.Garnett User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 Days 3 h 13 m 45 sec
Reputation Power: 4
Send a message via AIM to Geo.Garnett
dekoi

I got three questions for you.

Does this problem have to be done in C or can you use C++ as well?

Let me see if I got this correct, do you need to read this from a seperate text file or no?

and is this a problem to be done for school or is there no time frame? either way it doesn't matter just wondering.
__________________
---Official Member Of The Itsacon Fan Club---
Give a man a fish and he will eat for a day. Teach a man to fish and he will sit in a boat all day drinking beer.

Reply With Quote
  #3  
Old October 23rd, 2005, 04:18 PM
dekoi dekoi is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2005
Posts: 12 dekoi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 27 m 52 sec
Reputation Power: 0
Quote:
Originally Posted by Geo.Garnett
I got three questions for you.

Does this problem have to be done in C or can you use C++ as well?

Let me see if I got this correct, do you need to read this from a seperate text file or no?

and is this a problem to be done for school or is there no time frame? either way it doesn't matter just wondering.


C.

It reads the input data from a seperate file.

Due tuesday, at home.

I'm confused as to why this matters.

Reply With Quote
  #4  
Old October 23rd, 2005, 04:19 PM
Geo.Garnett's Avatar
Geo.Garnett Geo.Garnett is offline
Registered Loser
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Location: Retardation Nation...
Posts: 347 Geo.Garnett User rank is Private First Class (20 - 50 Reputation Level)Geo.Garnett User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 Days 3 h 13 m 45 sec
Reputation Power: 4
Send a message via AIM to Geo.Garnett
Well I briefly looked over your code and although I'm no expert, I look for compiler errors and try and solve the problem from there, first. So I solved the one problem it showed for the code and then I ran it. Then it had me enter 3 numbers and then computed an astronomical amount with a lot of zero's.

I have not even attempted this yet but you need to put more printf() or cout<< statements to let the user know what he's doing. Another suggestion would have more error testing or basically stopping the user from entering a incorrect amount.

Just some suggestions no solution, but Ill try and look over it again although Michaelsoft the guy you were getting help from is far more knowledgeable than I.

Reply With Quote
  #5  
Old October 23rd, 2005, 04:22 PM
Geo.Garnett's Avatar
Geo.Garnett Geo.Garnett is offline
Registered Loser
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Location: Retardation Nation...
Posts: 347 Geo.Garnett User rank is Private First Class (20 - 50 Reputation Level)Geo.Garnett User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 Days 3 h 13 m 45 sec
Reputation Power: 4
Send a message via AIM to Geo.Garnett
Well that only matters because I was going to re-write it in my own terms and I use a lot of stuff that is strictly C++ functions such as cout<< and cin>> statements that would not be compatable for C because they are usually used for C++ only.

And the time factor, well, I got homework of my own to do, so I was wondering if you got a little time for this assignment or if it was a personal project.

And one other thing, if you are reading from a seperate data file why is it that you never open it to be read? just a thought. and another thing that I will show you below, how come you have or statement inside the printed portion of the printf?

Code:

printf("Product #: %d || Price/Unit: $%.2f || # of Products: %d || 
Total: $%.2f\n", prod_num, final_unitprice, number, final_price_product);


if you want them to determine which is which you might want to consider placing them seperately.

These are all just suggestions to try and help you, I am only a student too, but Im just trying to help you out a little.

Last edited by Geo.Garnett : October 23rd, 2005 at 04:30 PM.

Reply With Quote
  #6  
Old October 23rd, 2005, 04:54 PM
B-Con's Avatar
B-Con B-Con is offline
:bcon: moderator
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Location: int main()
Posts: 351 B-Con User rank is Private First Class (20 - 50 Reputation Level)B-Con User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 23 h 8 m 6 sec
Reputation Power: 4
Quote:
Originally Posted by dekoi
However, I'm not sure what the assignment means. Firstly, is "passed" supposed to be "past"?

No, it's "passed". To "pass" something in this context means to provide it as an arguement to a function. For example, when you use the code
printf("hi");
you're passing the string "hi" as an arguement to the function printf()

Quote:
Also, what does it mean to "return a possibly modified price"? Can anyone give me an example of how to use functions?

To "return" a value means that, when the function exits, it can return a value to the function that called it. "possibly modified" in the question simply means that the value you return may or not be modified by the code it passes through.
__________________
Officially a member of the Itsacon fan club. Beer blasts are every friday at Viper_SB's house. I bring the chips.



Reply With Quote
  #7  
Old October 23rd, 2005, 04:57 PM
dekoi dekoi is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2005
Posts: 12 dekoi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 27 m 52 sec
Reputation Power: 0
You don't quite understand. The code works fine on one computer and doesn't properly run on another. The input file is opened using the redirection with my compiler. And im using esentially the same compiler on both computers.

Here is the output on the good version:

Quote:
---------------------------> PRODUCT [1]<---------------------------

Product #: 12376519 || Price/Unit: $34.94 || # of Products: 2 ||
Total: $69.89


---------------------------> PRODUCT [2]<---------------------------

Product #: 76324306 || Price/Unit: $49.00 || # of Products: 3 ||
Total: $147.00


---------------------------> PRODUCT [3]<---------------------------

Product #: 55956111 || Price/Unit: $17.59 || # of Products: 4 ||
Total: $70.35



The total price is: $287.24
The average price per product is: $31.92

Any key to return to Quincy...

Reply With Quote
  #8  
Old October 23rd, 2005, 04:58 PM
dekoi dekoi is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2005
Posts: 12 dekoi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 27 m 52 sec
Reputation Power: 0
Quote:
Originally Posted by B-Con
No, it's "passed". To "pass" something in this context means to provide it as an arguement to a function. For example, when you use the code
printf("hi");
you're passing the string "hi" as an arguement to the function printf()


To "return" a value means that, when the function exits, it can return a value to the function that called it. "possibly modified" in the question simply means that the value you return may or not be modified by the code it passes through.


Okay. Thank you. Im going to try something and get back to you.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > User defined functions


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT