JoBe 36 Posting Pro in Training

@ jwenting, thanks for the explanation, I have no doubt that what I wrote would be unusable in other examples, but fact is, I have to use ASCII :o

It's our assignement :!:

@ Narue, am I correct when I say that your code is written in OO :?:

Because this: map<char, int> get_frequency
and this: string::const_iterator begin = s.begin(); type of code, we haven't seen in structured programming.

We are going to start Object Oriented in one or two weeks :!:

Oh yes, going to order following books by the way:

- 3D Math Primer for Graphics and Game Development.
ISBN: 1556229119
- Essential Mathematics for Computer Graphics
ISBN: 1852333804
- Concrete Mathematics: A Foundation for Computer Science
ISBN: 0201558025
- Art of Computer Programming Volumes 1-3 Box set
ISBN: 0201558025

What do you think :?:

JoBe 36 Posting Pro in Training

Hi Narue,

Before I make suggestions, I'll give you a cool version:

Thanks for the example, but you know, I prefere to try and write my own ;)

On to the code!

>#include <stdafx.h>
This should be a avoided because it makes your code nonportable. To be more specific, you can only compile code using stdafx.h with Visual Studio AFAIK.

Thanks for the tip, I'll keep that in mind :!:

>#include <iostream.h>
>#include <iomanip.h>
These are old headers that technically aren't a part of C++. Remove the .h extension and you'll have the standard headers, but don'e forget to qualify standard names with std::.

Now that is strange, we're being teached this way of programming and you say that it's the old way, can you explain this a bit more :?:

>void main(void)
int main(). main never has, and probably never will return void. You can safely omit the void parameter because it means the same thing as an empty list in C++ and it's kind of ugly.

So, if I understand correctly, it's better to start writing main with 'int main()' can you explain why because I'm not asking a return from main :?:

>letter++;
This relies too much on the character set. ASCII is not the only character set in existence, and some don't have the alphabet in sequential order. On such character sets your code will break.

Well, we we're told to use ASCII, so :o I'll …

JoBe 36 Posting Pro in Training

Hi guys,

Just wanted to ask you're opinions about this program I had to write for evening school, the idea is when you enter a phrase wich isn't longer then 80 letters, the programm will count the amount of each letter wich is written :!:

It doesn't have to keep account of 'a' or 'A', and I had to show each letter of the alphabet, just in case you we're wondering.

So, if you could just give me some pointers ;) in wich way I could improve it, I would appreciate very much :)

// AantalLetters.cpp : Johan Berntzen: 11/11/2004

#include <stdafx.h>
#include <iostream.h>
#include <iomanip.h>

int zoek(const char s[], char ch);

void main(void)
{
    int amount=0;
    char str[80], letter='a';

    cout<< " Write a phrase: ";
    cin.getline(str,80);

    for (int i=0;i<26;i++)
    {
        amount=zoek(str, letter);
        cout<< letter << " = "<< amount;
        cout<<setw(8);
        if(i%7==6)cout<<endl;
        letter++;
    }
    cin.get();
}

int zoek(const char s[], char ch)
{
    short a=0;

    for (int i=0;i<s[i];i++)
    {
        if (s[i]==ch)
            a=++a;
    }
    return a;
}
Dave Sinkula commented: Use code tags. +0
JoBe 36 Posting Pro in Training

Thanks Narue,

Found them at Amazon :p

JoBe 36 Posting Pro in Training

>what are algorithms
I have a dozen or so algorithm books focusing on C++ alone. ;)

Well, could you share there name and maybe ISBN number then :mrgreen:

Thanks ;)

JoBe 36 Posting Pro in Training

Euh, Narue, what are algorithms :eek:

Aren't algorithms used in computer AI :?: If so, then yes, those kind of formulas I certainly would love to have a book from ;)

But also for graphics you know, so, instead of using a 3D drawing program, I could use C++ to make drawings :!:

JoBe 36 Posting Pro in Training

Can you be more specific as to what formulae you're talking about? Otherwise I'll just direct you to the mathematics section of your local bookstore.

Hi Narue,

Well, good question, I was wondering wether there is a book wich has all or most formulas in it wich are used in C++. They would be from the simpliest to the very difficult ones :!:
For instance, one that shows the formula for this kind of calculation:
- Show with wich atleast two consequetive numbers you can get 15?

Solution:
1 2 3 4 5
4 5 6
7 8

Now, I presume to get these numbers, there are mathematical formulas right :?:

JoBe 36 Posting Pro in Training

Hi,

I just wanted to ask if someone could give me a name of a good book in wich the mathematical formulas are written down with some explanation about them :?:

Reason I ask is, I'm going to evening school to learn C++ but in using C++ I encounter very regularly mathematical formulas wich I either don't know, or haven't used for a long, long time (almost 20 years)

That's why it would be easier if I would have a book in wich they are written down and explained in :!:

Thanks for the help guys, if this is asked in the wrong forum , my apologies then :o

JoBe 36 Posting Pro in Training

Hi guys,

Thanks for the quick responce, it was a combination of both, using your selection gallas and setting the variable s to 100 solved the problem.

Thanks guys, I don't suppose it would work without s being given a certain value that is equal to x :?:

JoBe 36 Posting Pro in Training

Hi guys,

Quick question, I was trying out to determine the second smallest number of 10 random entered numbers, this is what I got:

#include <stdafx.h>
#include <iostream>
#include <iomanip>
using namespace std;

int main(void)
{
int s=0,x=0,y=0;

cout<< "Input ten random numbers with cin and determine the second smallest number"<< endl;

for(int i=0;i<10;i++)
{	

	cin>> x;cin.get();
	if (x<s)
	{
		s=x;
		if (s<y)
		{
		y=s;
		}
	}
}

cout<< "The second smallest number is: " << y << endl;
cin.get();

	return (0);
}

Problem is that s equals zero, and even if I change that, I'm not getting the result I wanted :)

Can any of you guys give me some advice, this is just something that I'm trying out for myself and is not evening school related :!:

Thanks,
JoBe

JoBe 36 Posting Pro in Training

I really had trouble in finding the solution, Ive tried to find a solution on my own, but somehow I just couldn't see how I had to do it :-|

The srand ( , I saw this a bit to late :lol:

I don't only know how to solve the problem now, but also understand why you did it, thanks again :!:

JoBe 36 Posting Pro in Training

Thanks very much Dave :p

This way I can see how you did it, and find a way to do it in my programm :)

Got a few questions tough, I don't want to just copy your solution without knowing why you did certain things and what the meaning of some of them is :!:

1) srand(time(NULL)); Ive read in a tutorial on this forum about it, does this make certain that the random numbers are allways different? Why are there two )'s at the end, think this was not intentional right :?:

2) sizeof array / sizeof *array, can you give me a brief explanation why you divide these :?:

3) REDO, I understand the English words and can see what it does, but can you tell me what it is :?: Meaning, could I use another word to get my variable into the iteration again :?: IT's probably and most certainly the goto that does it right, but just want to be certain :)

4) When you use array[ j ], this is just the next number in the array wich comes after [ i ] correct and could you use something like this:

if ( array == array -1) because this way you are referring to the previous number in the array aswell correct :?:

Anyway, thanks again for the example, it's going to help alot, if you don't have time to explain these questions, I understand :!:

JoBe 36 Posting Pro in Training

Hi Ejaz,

Thanks for the quick reply, but can you give me an example how I should write it because that's the problem, I understand that I have to use an iteration to control every single N., but how do I write this combination of iteration and selection :?:

Thanks

JoBe 36 Posting Pro in Training

Hi ladies and gentlemen,

Gotta question,

I have a program in wich I make a small array of 100 random numbers wich are smaller then 1000!

Like this:

# define MAX 100

void srand(int tabel[MAX]);

void srand(int tabel[MAX])
    {
        for (int i=0;i<MAX;i++)
            do
            tabel[i]=rand();
            while (tabel[i]>1000);  
    }

This of course is only one part of the program and as you can see this array is made with a function! Now, this way I get random numbers but there are several numbers in that array wich are shown twice or even more times, how can I change this program so it doesn't use the same number twice?

Can anyone please help me out?

Thanks in advance!

JoBe 36 Posting Pro in Training

So, if I understand you correctly, you're saying it's best to write a function for every operation that is needed and not to combine several operations in one function?

Sorry to ask a question you've explained, I'm not native English speaking and don't want to miss interpretate your reply :o

JoBe 36 Posting Pro in Training

Thanks Narue :p

You saved my day :cheesy:

Anyway, it's like you said, I didn't check the code because I only wanted you to understand what I was trying to do, the code I have written is only a small part of the code I need.

Idea is to use several funtions in combination with arrays and that those functions get called upon in the main program and you have to write several functions to do so.

Wich ones?
1) Make an array with 20 numbers filled by keyboartd(cin ;) ) and check two things, when zero is entered by cin -> break, when array is full -> stop the function. I believe I can do this in one function, right :?:

2) When 10 numbers are entered by keyboard, return to next line, this can be done by: cout<<setw(5)<<list;
if(i%10==9)cout<<endl;

3) Ask question, "Do you want to make another array?", this also can be done by a small funtion wich I don't have a problem in handling!

Anyway, if your able, could you give me your opinion on this and thanks again for the help, much appreciated!

JoBe 36 Posting Pro in Training

Hi everyone,

Ive got a question concerning an excersise I have to make in wich I use an array in a function:

I have to manually fill an array with twenty numbers randomly and the array has to be used by a function, now, I know how to do this automatically like this:

void settabel (int tabel[MAX]);

void main (void)
{
settabel(lijst):
}
void settabel int tabel[MAX])
{
for int (int i=0; i<MAX; i++)tabel=i+1;
]

In this case, the array gets filled automatically from 1 to 20, that's not a problem!

But how on earth can I use this type of function and manually with cin>> get that array filled with numbers??????

Thanks for any advice and help :!:

JoBe 36 Posting Pro in Training

@ DoubleShot and fahad,

Solution to the problem was:

tot_int = interest;
    eindk = begink;



while (tijd < per) 
                     {
                      [COLOR=DarkOrange]tot_int[/COLOR] = (eindk * perc) / 100;
                      eindk = eindk + [COLOR=DarkOrange]tot_int[/COLOR];
                      tijd ++;
                     }

Problem was that I tought once you put 'interest' into 'tot_int' and the same for 'begink' into 'eindk', they would automatically be adjusted to the new value, unfortunatly that wasn't the case and I had to use eindk and tot_int in the iteration :o

Anyway, thanks again for the help guys, it was due to what you guys wrote and said that I found the solution :D

JoBe 36 Posting Pro in Training

Thanks for the tips DoubleShot, I'll surely will try them out :D

Thing is, in 'cin' can enter the 'begink' and this will automatically put the same amount into 'eindk'

Same for 'interest' into 'tot_int'

What I have to do is, calculate the total amount of money and interest after a certain period, wich is represented by 'per' And that was the problem, Ive allready changed this:

tot_int = interest;
eindk = begink;

while (tijd < per)                                   {
        interest = (eindk * perc) / 100;
        eindk = eindk + interest;
        tijd ++;
        }

Now, eindk is calculated in the correct manner, I only need to change it for the tot_int wich still remains at zero :o

Again, thanks for the tips, Ive allready corrected the initialization for the float variables.

A big thanks to fahad aswell for the mail you send me, I'll check it out and let you know what happend ok ;)

I'll let you guys now how it turns out by tomorrow ok!

JoBe 36 Posting Pro in Training

Hello ladies and gentlemen,

This is the program:

/* berekening_kapitaal.cpp : Defines the entry point for the console application.*/


#include <iostream.h>

void main(void)                     
{ 
    short int per = 0, tijd = 0;                                             
    float begink = 0, interest = 0, perc = 0, tot_int = 0, eindk = 0, kapitaal = 0;


    cout << "Als het begin kapitaal gelijk is aan: "; 
    cin >> begink; cin.get();
    cout << "En het percentage is ";
    cin >> perc; cin.get();
    cout << "Over een periode van: ";
    cin >> per; cin.get();

    tot_int = interest;
    eindk = kapitaal;

        while (tijd < per)
        {
        interest = (kapitaal * perc) / 100;
        kapitaal = kapitaal + interest;
        tijd ++;
        }

    cout << "Dan is het eindkapitaal na " << per << " jaar gelijk aan "   << eindk << " Euro " << endl;
    cout << "De samengestelde intresten bedragen dan " << tot_int << " %" << endl;

    cin.get();
}

The problem is that when I enter values for begink, interest and per, and try to calculate << eindk << and << tot-int <<!

My iteration does not function, can someone help me in explaining what I'm doing wrong in that iteration!

Thanks for the help,