Technical Discussion
  >> Web Design / HTML / Web hosting Forum


Register (or login) on our website and you will not see this ad.


These posts have been archived and can no longer be replied to or modified.
Pages in this thread: 1 | 2 | 3 | 4 | (show all)   Print Thread
Standard User deleted
(deleted) Wed 09-Dec-09 14:18:09
Print Post

C++ coding


[link to this post]
 
Hi folks,

My university course has progressed onto C++ programming now, and im learning how to use various variables and functions within the program using Dev CPP compiler. If there is any coders out there, I would appreciate your help on this nagging issue.

Basically the first part of the program I have to calculate call charges for customers, charged at national rate at £1.50 a minute for the first 3 minutes, and 30p thereafter. However at certain days and times, theres discounts applied. For example between 8am and 4:59pm on weekends theres a 60% discount. I have been searching all over the net for C++ maths algorithms but couldn't find anything relevant.

Thanks for any help.
Standard User wrtpeeps
(eat-sleep-adslguide) Wed 09-Dec-09 14:23:52
Print Post

Re: C++ coding


[re: deleted] [link to this post]
 
Write your own. I guess that's what you're meant to do?

Signature.
Standard User deleted
(deleted) Wed 09-Dec-09 14:26:11
Print Post

Re: C++ coding


[re: deleted] [link to this post]
 
I'm not a C++ dev, but percentage calculations are the same no matter what language you are using..

Here's a PDF with the relevant formulas..


Register (or login) on our website and you will not see this ad.

Standard User deleted
(deleted) Wed 09-Dec-09 14:32:37
Print Post

Re: C++ coding


[re: deleted] [link to this post]
 
I don't think there's anything specifically C++ about the algorithms needed. Just think about how you would do it by hand, which seems fairly straightforward, and then convert that into code. Ideally, as it's a C++ course, you'll use classes and inheritance for your solution but I'm not sure what point you have reached in your course.

Just forget the programming language to start with and try and think about how you would actually solve the problem in less specific terms.

The details? Well, that's the point of your being set the exercise, isn't it?
Standard User deleted
(deleted) Wed 09-Dec-09 14:34:29
Print Post

Re: C++ coding


[re: deleted] [link to this post]
 
Try several nested IF Statements (or CASE Statements if you prefer).

Don't forget it is good practice to always put the curly brackets inside any IF even if it only has one line of code following it.
Standard User deleted
(deleted) Wed 09-Dec-09 14:37:42
Print Post

Re: C++ coding


[re: deleted] [link to this post]
 
Not good practice in C++! You really should be using polymorphism rather than case statements; that way it's easy to add in new rating bands. But it depends upon how advanced the C++ course currently is.
Standard User deleted
(deleted) Wed 09-Dec-09 14:42:31
Print Post

Re: C++ coding


[re: deleted] [link to this post]
 
Thank you for the assistance folks, I am writing this stuff down to try and make sense of it. I apologise if you thought I was asking someone to do the program for me, I wasn't I just want to get my head round the algorithm I could possibly use for these sort of tasks.

In regards to the progress this is the 9th week of C++ so I would class myself as a beginner currently.
Standard User wrtpeeps
(eat-sleep-adslguide) Wed 09-Dec-09 16:45:02
Print Post

Re: C++ coding


[re: deleted] [link to this post]
 
It's not really a c++ specific function, more just maths stuff.

Signature.
Standard User Kenneth
(legend) Wed 09-Dec-09 19:15:13
Print Post

Re: C++ coding


[re: deleted] [link to this post]
 
This is not really a mathematics problem, it is more of a logic problem.

First I'd start with some questions (or state assumptions since education exercise)
  • do they charge a whole minute, irrespective of whether 1 second or 60 seconds of minute used or do they pro-rata rate?
  • Does the discount kick in if the call starts before discount period but then goes into discount period? or do they not allow discount if some of call outside discount period


I think it might help you to make problem this visual - e.g. create a spreadsheet with columns
  • Time
  • Call minute
  • Minute Cost
  • Discount Rate
  • Actual Cost


Then build up a call�s cost minute by minute and calculate the costs -
e.g.

7:50

Text
1
23
4
07:57 _ 1 _ �1:50 _ 60% _ *
07:58 _ 2 _ �1:50 _ 60% _ *07:59 _ 3 _ �1:50 _ 60% _ *
08:00 _ 4 _ �0:30 _ 00% _ *


* I leave those value to you

This will allow you to build up some test cases, so you can check your process works correctly. You need to be especially careful of are when you hit boundaries - the change of rates and change of day being the main ones (and it is possible one call may span more than one), so build up some cases covering those

Hopefully these will give you pointers on how to tackle the problem

Ken

When arguing with a fool, pause and ponder - are they also arguing with a fool
Standard User deleted
(deleted) Wed 09-Dec-09 22:30:17
Print Post

Re: C++ coding


[re: deleted] [link to this post]
 
In reply to a post by lt567:
In regards to the progress this is the 9th week of C++ so I would class myself as a beginner currently.
they are teaching you c++ as a first language? that sucks. good luck.
Standard User wrtpeeps
(eat-sleep-adslguide) Thu 10-Dec-09 00:33:24
Print Post

Re: C++ coding


[re: deleted] [link to this post]
 
In reply to a post by 12eason:
In reply to a post by lt567:
In regards to the progress this is the 9th week of C++ so I would class myself as a beginner currently.
they are teaching you c++ as a first language? that sucks. good luck.


I'd agree with this regarding the suck bit.

Signature.
Standard User Kenneth
(legend) Thu 10-Dec-09 08:20:49
Print Post

Re: C++ coding


[re: deleted] [link to this post]
 
seems a good choice of first language - you learn everything about pointers etc., then you can really appreciate languages like Java/C# and what you don't have to do wink

it could be worse though - they could be teaching C++ on MFC

Ken

When arguing with a fool, pause and ponder - are they also arguing with a fool
Standard User Kenneth
(legend) Thu 10-Dec-09 08:23:04
Print Post

Re: C++ coding


[re: deleted] [link to this post]
 
In reply to a post by AnotherExPipex:
Ideally, as it's a C++ course, you'll use classes and inheritance for your solution

Only when it makes the code easier to understand!

Ken

When arguing with a fool, pause and ponder - are they also arguing with a fool
Standard User deleted
(deleted) Thu 10-Dec-09 08:50:56
Print Post

Re: C++ coding


[re: Kenneth] [link to this post]
 
I think there's a little more to OO than making the code easy to understand (some might argue that it does the opposite). Ease of maintenance and the ease with which new features can be added (plus protection of data) are more important IMO.
Standard User deleted
(deleted) Thu 10-Dec-09 08:55:23
Print Post

Re: C++ coding


[re: deleted] [link to this post]
 
I'd say C++ is a very fine language to learn programming, particularly when well taught. It can be as simple or as complicated as you like, and it has very good OO features. I'm struggling to think of a better one - possibly Pascal for really basic stuff, but it's not very useful in the modern day and there's no real standard for OO in Pascal.

Actually I'd probably choose Smalltalk, but I suspect many would disagree with me. Objective C and Eiffel are also good, but very non-mainstream.
Standard User Kenneth
(legend) Thu 10-Dec-09 09:02:33
Print Post

Re: C++ coding


[re: deleted] [link to this post]
 
In reply to a post by AnotherExPipex:
I think there's a little more to OO than making the code easy to understand (some might argue that it does the opposite). Ease of maintenance and the ease with which new features can be added (plus protection of data) are more important IMO.


And those of us who maintain code would argue - over using/badly designed OO makes the code slower, less reliable, more obtuse and hard to maintain

properly applied OO - does the resverse, so I would argue if OO makes your code harder to understand (once you understand OO) you are doing it wrong!

Ken

When arguing with a fool, pause and ponder - are they also arguing with a fool
Standard User deleted
(deleted) Thu 10-Dec-09 09:14:41
Print Post

Re: C++ coding


[re: Kenneth] [link to this post]
 
over using/badly designed OO makes the code slower, less reliable, more obtuse and hard to maintain
I'd say that's true of any programming language. The Obfuscated C contest comes to mind.
Standard User Kenneth
(legend) Thu 10-Dec-09 09:28:45
Print Post

Re: C++ coding


[re: deleted] [link to this post]
 
it does - but there seems to be something about inheritance (or any new technique they discover) that coders seem to think they must use it as much as possible whether it helps or not crazy

in the problem mentioned - a couple of functions could solve it quite elegantly, now I'd wrap those up into an object as they would be part of a bigger problem

Ken

When arguing with a fool, pause and ponder - are they also arguing with a fool
Standard User Kenneth
(legend) Thu 10-Dec-09 09:34:30
Print Post

Re: C++ coding


[re: deleted] [link to this post]
 
I'd say some thing like Java or C# would be best learning languages now - but lack of teaching pointers and memory issues may be a downside

Outside education - the only major OO Pascal choice is Delphi and when only one choice no need of standards - but that has unfortunately become rather dead in the water frown

Ken

When arguing with a fool, pause and ponder - are they also arguing with a fool
Standard User wrtpeeps
(eat-sleep-adslguide) Thu 10-Dec-09 10:14:26
Print Post

Re: C++ coding


[re: Kenneth] [link to this post]
 
C# has pointers. You're discouraged from using them and they're classified as "unsafe" I believe, but they're there none the less.

Signature.
Standard User deleted
(deleted) Thu 10-Dec-09 11:30:05
Print Post

Re: C++ coding


[re: Kenneth] [link to this post]
 
but lack of teaching pointers and memory issues may be a downside
Proponents of those languages would say those were plus points rather than downers! My only problem with C# is the fact that it is - essentially (I know about Mono and all that) - limited to one platform.

I think you'll find that Smalltalk is still quite widely used (in the financial sector?) in the real world. Delphi, in its day, was great (even better was C++ Builder) but, as you say, a little past it now. I do like Smalltalk; everything is an object, even classes and metaclasses.
Standard User deleted
(deleted) Thu 10-Dec-09 11:35:24
Print Post

Re: C++ coding


[re: Kenneth] [link to this post]
 
There is a temptation to use inheritance when "borrowing" as they call it in Smalltalk - embedding an instance of an object inside another object is a better choice. That's down to getting the initial model right.

The problem given is trivial so any form of OO could be considered OTT. I guess it depends upon whether the excercise was set as a general programming problem or a specific C++ problem. 9 weeks into a course I'd guess they'd have started on classes so it may be that it was set as an extremely simple example of how to use polymorphism rather than switch, or nested if, statements.
Standard User deleted
(deleted) Thu 10-Dec-09 11:41:31
Print Post

Re: C++ coding


[re: Kenneth] [link to this post]
 
I found out that say if a call was made at 11:45 PM on Monday and finished at 12:10 AM (Program would also say called ended Tuesday), the discount will be applied to the whole call and not just the minutes that went over.

The way the course works is that its C++ first, followed by VB.net and finishing with Java.
Standard User Kenneth
(legend) Thu 10-Dec-09 12:08:46
Print Post

Re: C++ coding


[re: deleted] [link to this post]
 
In reply to a post by AnotherExPipex:
but lack of teaching pointers and memory issues may be a downside
Proponents of those languages would say those were plus points rather than downers!

When writing code yes - but when learning to program I think it is important for coders to understand what is happening below the hood so to speak - and it doesn't hurt to have to learn an element of displine that using pointers needs

In reply to a post by AnotherExPipex:
My only problem with C# is the fact that it is - essentially (I know about Mono and all that) - limited to one platform.


I am not sure that is a major issue for teaching people to code (a good coder should be able to apply their skills to learning other languages) - as long as they don't have to pay for visual studio that is

In reply to a post by AnotherExPipex:
I think you'll find that Smalltalk is still quite widely used (in the financial sector?)

I thought Java had pretty much taken over finance
In reply to a post by AnotherExPipex:
in the real world. Delphi, in its day, was great (even better was C++ Builder) but, as you say, a little past it now.

I am not sure past it - more out of fashion (not that it was ever really super fashionable)

The Win32 version of Delphi could do with some language updates - such as iterator for loops and template classes, but I think the .Net version (Delphi Prism) is getting such things as well as Free Pascal - though I think I'll probably be doing Java a lot more from now on. C++ builder on VCL is defintely better than MS C++ & MFC

Ken

When arguing with a fool, pause and ponder - are they also arguing with a fool
Standard User Kenneth
(legend) Thu 10-Dec-09 12:11:04
Print Post

Re: C++ coding


[re: wrtpeeps] [link to this post]
 
yes - but they aren't (shouldn't be) part of day to day coding in C#, merely there to allow interactions with APIs etc.

Ken

When arguing with a fool, pause and ponder - are they also arguing with a fool
Standard User deleted
(deleted) Thu 10-Dec-09 14:24:26
Print Post

Re: C++ coding


[re: deleted] [link to this post]
 
Javascript is the best first language to learn. It's c based, loosely typed and is incredibly flexible. You can learn the basics of language structure and can get quick results from it. On top of that, to really use it well, you have to learn about prototypical inheritance, how that differs from classical languages and how you can simulate classical inheritance with it. It's a great way to learn OO. Once you've understood all that, moving on to C# is easy. It' is just a classical version of javascript, the syntax is almost identical(if a little more verbal in places).

This is the route I'm been taking, and it's pretty rewarding. Sure, you can learn the simple stuff in c++ before moving onto OO later, but you can't get any real results from it, just snippets of useless code. With javascript, you can write fairly effective Firefox add-ons with simple code, with c# you can make useful office addons and form apps, again with simple code. I remember reading through a C++ book about 5 years ago (principles and practice) and it was enough to put me off programming altogether.

Edited by deleted (Thu 10-Dec-09 14:25:58)

Standard User wrtpeeps
(eat-sleep-adslguide) Thu 10-Dec-09 15:28:50
Print Post

Re: C++ coding


[re: Kenneth] [link to this post]
 
In reply to a post by Kenneth:
yes - but they aren't (shouldn't be) part of day to day coding in C#, merely there to allow interactions with APIs etc.


Yea, if you're using pointers regularly you're doing something wrong, but they are available to use if you want.

Signature.
Standard User deleted
(deleted) Thu 10-Dec-09 16:26:06
Print Post

Re: C++ coding


[re: deleted] [link to this post]
 
Javascript is the best first language to learn.
I think that tells me all I need to know.
Standard User Kenneth
(legend) Thu 10-Dec-09 16:28:26
Print Post

Re: C++ coding


[re: deleted] [link to this post]
 
In reply to a post by AnotherExPipex:
I think that tells me all I need to know.

Could have been worse - he could have said Ruby wink

Ken

When arguing with a fool, pause and ponder - are they also arguing with a fool
Standard User deleted
(deleted) Thu 10-Dec-09 16:38:42
Print Post

Re: C++ coding


[re: Kenneth] [link to this post]
 
... or even Perl. Fine languages in the correct context, but not for teaching purposes.
Standard User deleted
(deleted) Thu 10-Dec-09 16:45:11
Print Post

Re: C++ coding


[re: deleted] [link to this post]
 
js is the best language ever made, despite its flaws.
Standard User deleted
(deleted) Thu 10-Dec-09 16:47:34
Print Post

Re: C++ coding


[re: deleted] [link to this post]
 
It doesn't surprise me to hear you say that.wink
Standard User deleted
(deleted) Thu 10-Dec-09 16:57:42
Print Post

Re: C++ coding


[re: deleted] [link to this post]
 
In reply to a post by AnotherExPipex:
but not for teaching purposes.
And by that, you mean 'you need to learn how a computer works on a hardware level to work with c++ effectively.' so as a language to teach someone doing a compsci degree, it's perfect. But for a first language, it sucks, because you are better off learning the fundamentals of programming syntax without worrying about data types and pointers etc. By mastering simple languanges first, you can then dive into learning C++ with also having to learn programming too, if you catch my drift. Tthe fact that the OP is having trouble with basic coding, coding that is independent from C++ as a language, suggests that they skipped courses that you'd usually take at A-level. I certainly remember the first thing we did in A-level computing was learn pascal.
Standard User deleted
(deleted) Thu 10-Dec-09 17:08:26
Print Post

Re: C++ coding


[re: deleted] [link to this post]
 
In reply to a post by AnotherExPipex:
It doesn't surprise me to hear you say that.wink
just take one simple example. Arrays. In js, you can do var a = [], then push(), pop(), sort(), join(), slice().. you can do everything and anything with it, and what you can't do you can simple add by making your own method. Compare that to c#. The standard array is so inflexible, ms had to add new array types in .net, and even then they made it overly verbose. You need lists and sorted lists, stacks and queues. Each one has their own methods, that you can't use one the other. It's lame.

A lot of languages could learn a thing or two from js.

Edited by deleted (Thu 10-Dec-09 17:10:10)

Standard User deleted
(deleted) Thu 10-Dec-09 17:17:08
Print Post

Re: C++ coding


[re: deleted] [link to this post]
 
Sounds like you need Smalltalk - far more flexible.
Standard User deleted
(deleted) Thu 10-Dec-09 17:18:36
Print Post

Re: C++ coding


[re: deleted] [link to this post]
 
And by that, you mean 'you need to learn how a computer works on a hardware level to work with c++ effectively.'
No I don't.
Standard User Kenneth
(legend) Thu 10-Dec-09 18:44:39
Print Post

Re: C++ coding


[re: deleted] [link to this post]
 
In reply to a post by AnotherExPipex:
... or even Perl.


1987 - Larry Wall falls asleep and hits Larry Wall's forehead on the keyboard. Upon waking Larry Wall decides that the string of characters on Larry Wall's monitor isn't random but an example program in a programming language that God wants His prophet, Larry Wall, to design. Perl is born

....

1995 - Brendan Eich reads up on every mistake ever made in designing a programming language, invents a few more, and creates LiveScript. Later, in an effort to cash in on the popularity of Java the language is renamed JavaScript.


Ken

When arguing with a fool, pause and ponder - are they also arguing with a fool
Standard User deleted
(deleted) Thu 10-Dec-09 18:52:39
Print Post

Re: C++ coding


[re: Kenneth] [link to this post]
 
In reply to a post by Kenneth:
1987 - Larry Wall
1995 - Brendan Eich reads up on every mistake ever made in designing a programming language, invents a few more, and creates LiveScript. Later, in an effort to cash in on the popularity of Java the language is renamed JavaScript.
The only reason ecmascript sucks so much is that loads of java devs ruined it early on, and microsoft kept all the bugs in it for 'backwards compatibility'.
Standard User deleted
(deleted) Thu 10-Dec-09 19:44:27
Print Post

Re: C++ coding


[re: deleted] [link to this post]
 
Of course, the other main disadvantage to learning C++ as a first language, is that you'll become a huge snob.
Standard User deleted
(deleted) Thu 10-Dec-09 20:22:42
Print Post

Re: C++ coding


[re: deleted] [link to this post]
 
Miaow!

smile
Pages in this thread: 1 | 2 | 3 | 4 | (show all)   Print Thread

Jump to