Learning C++

<< Previous Page 2 of 2

Forum Index -> General

Australia September 22 2010 08:46Posts: 99

bayou wrote:

I'm probably just confusing the situation, but what the hell. Note you don't have to forward-declare functions in a header file, or in fact at all. Like Ratfink said, the compiler reads through the file from top to bottom, and you can't have a call to a function it doesn't know about yet. You can define/declare the function in one go so long as it appears before your call to it.

Bayou is entirely correct; thanks for clarifying that. However, my spin on it is that I prefer to not care what order my functions are defined in (as is the behaviour in my favourite language: C#). Writing an .h file, while irritatingly tedious, allows you to ignore the order that functions are defined in. But at the end of the day (unless you're sharing code) it's up to you as to how you want to handle it.
Charity, if you have the means, is a personal choice, but charity which is expected or compelled is simply a polite word for slavery. ADMIN
United States Rostered Member for Proxiteam September 22 2010 10:41Posts: 619
Thanks a lot for the help guys. I know I'll be needing more help as the quarter progresses so I will continue asking questions as new problems or ideas come up. Once again, thanks.
http://sc2sig.com/s/us/1614629-1.png NEWSWRITER
United States Rostered Member for Proxiteam September 27 2010 15:23Posts: 619
Hello everyone. I was able to finish the homework this week(had to write a function in C++ for finding the average and median of an array using pointers). However now my professor has given us an extra credit question...he has given us 1 day to finish it(dont know the time for you Aussies but it is roughly 32 hours from this post). Now I have to write a function to find the mode.

I got out my whiteboard and started thinking about this and I am pretty stuck. The purpose of this assignment is to help us practice pointers and dynamic memory allocation. He also wants us to return the array in a special format but that is the least of my worries. However the format does have to do with how I solve this function so I will post it either away.

Form of the array that should be returned is:

[#modes, frequency of their appearance, mode1, mode2, mode 3, etc...]

Size of the returned array should be the number of modes + 2.


With that being said I have to keep count of three things: how many modes there are, how many times they appear, and then the actual modes.

I just got started on this function and I'll be working on it for majority of the day but my first question is:

How exactly should I go around recording the modes into a variable? I will not know how many modes there are when the program starts, so as this function runs, I need the computer to store the memory for me. This is the part that I am currently stuck on.

For example, if there are 2 modes then I will need 2 variables to keep track of each mode, but the thing is I do not know how many there will be. I know this all has to do with dynamic memory allocation(going to use DMA for short :P) and I do understand the concept behind DMA but putting it into practice is another thing.

I think what I am mostly looking for is a little 'push' into the right direction to help get me started. Not looking for the answer :P.

Thank you in advance for the help.
http://sc2sig.com/s/us/1614629-1.png NEWSWRITER
Australia Rostered Member for Proxiteam September 27 2010 20:26Posts: 1000
I'm not sure why you mention finding the mode (singular) in your first paragraph, but then talk about multiple modes (plural) later.

If you have only one set of data, surely there will only be one mode? (My understanding is that 'mode' is determined by the value that appears most frequently in the given set).

Just trying to understand the task.
aka swAMi. Melbourne! ADMIN
United States Rostered Member for Proxiteam September 27 2010 21:14Posts: 619
Yes that is true but say you have any array that consist of the following integers:

{1, 2, 3, 1, 2, 3, 1, 2, 3, 4 }

Then your modes will be 1, 2, and 3 because they all appear the same amount of time.
http://sc2sig.com/s/us/1614629-1.png NEWSWRITER
Australia September 27 2010 21:18Posts: 99
Sounds like you need an array that can grow dynamically as you add shit to it. Thankfully, it's called std::vector.

However, if your professor won't let you use vector (as he might want you to do it manually), then you'll have to allocate an array, and then grow it yourself. Growing is as simple as creating a new bigger array and then copying all the old data into it and then deleting the old array. This is what vector does internally (however, it is likely to grow the array larger than you need it so it doesn't have to grow every time you add to it).
Charity, if you have the means, is a personal choice, but charity which is expected or compelled is simply a polite word for slavery. ADMIN
Australia September 27 2010 21:47Posts: 225
I'm familiar with the following coding:

/ignore newbs
/away redtube
/dnd stfu
/me floats his own boat.
/f m everyone probe rush kang
TLDR
Australia Rostered Member for Proxiteam September 28 2010 01:42Posts: 427

/f m everyone probe rush kang

LMAO! good times :D
Timmy - "Making carriers really is a useful talent toi have!"
United States Rostered Member for Proxiteam September 28 2010 21:29Posts: 619
Awesome Ratfink! I actually just finished the program and hopefully it gets the extra credit I want :).

I actually took your advice and looked up vectors in C++(I love using vectors in Java) and I asked my professor if we HAD to use arrays. He gave me the benefit of the doubt in his Austrian accent and allowed me to do so. The code was not as tough as I had predicted but thanks Ratfink for pointing me in the right direction; had no idea C++ had vectors ;).
http://sc2sig.com/s/us/1614629-1.png NEWSWRITER
Australia September 29 2010 04:47Posts: 99
No problem. C++'s STL is really invaluable (as is any base class library for a language), though it can sometimes be a little esoteric.
Charity, if you have the means, is a personal choice, but charity which is expected or compelled is simply a polite word for slavery. ADMIN
United States Rostered Member for Proxiteam October 06 2010 15:27Posts: 619
This is a problem that has nothing to do with a homework assignment.

-------------

Professor hasn't formally assigned homework for the past 2 weeks so I've just been reading the chapters and doing the problems they give at the end of every chapter. I was doing a problem regarding movies and entering data, blah blah blah. It is supposed to give practice for structures.

The question I have has nothing to do with the given problem but rather getting input in general. Here is the code:

//code been deleted

The program works fine when I input values into it. However, let's say I want to input "Forrest Gump" into the name of the movie, it assigns "Forrest" to the name of the movie and "Gump" to the director of the movie. What is the correct way to assign "Forrest Gump" to the variable movieName. I can't seem to figure it out and my textbook doesn't supply the solution.

EDIT: Actually figured it out. Had to use the cin.getline method. I'll go ahead and delete the code I post for the sake of space lol. However I got a different question. Here is the code:
// Practice problems from Tony Gaddis Book
// Involves problems 11.4 - 11.8

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

struct Product
{
char description[50]; //Product description
int partNum; //Part Number
double cost; //Product cost
};


int _tmain(int argc, _TCHAR* argv[])
{

///Part 11.4 of the series
Product product1[100];

//Part 11.5 of the series
for(int f = 0; f < 100; f++)
{
product1[f].description[f] = ' ';
product1[f].cost=0;
product1[f].partNum=0;
}

//Part 11.6 of the series

product1[0].description = 'Claw Hammer';
product1[0].partNum = 547;
product1[0].cost = 8.29;

//Part 11.7 of the series
for(int j = 0; j < 100; j++)
{
cout << "Item: #" << (j+1);
cout << "\n Name:" << product1[j].description;
cout << "\n Cost: $" << product1[j].cost;
cout << "\n Part Number: #" << product1[j].partNum;
}

return 0;
}

The code in red is the line of code I'm having issues with. The error it gives me is that is there are too many characters in the constant. All I'm trying to do is assignt 'Claw Hammers' to the first element of the array product. Help please?
http://sc2sig.com/s/us/1614629-1.png NEWSWRITER
Australia October 06 2010 19:51Posts: 8
I believe the problem is you're using single quotes, where you need to use double quotes. A literal in single quotes is a single character, whereas a literal in double quotes is a string of characters.
Let's settle this like men. You. Me. Steppes Of War. Now.
United States Rostered Member for Proxiteam October 06 2010 19:54Posts: 619
By the way i'm not sure what happened to my post...
http://sc2sig.com/s/us/1614629-1.png NEWSWRITER
United States Rostered Member for Proxiteam October 06 2010 21:58Posts: 619

bayou wrote:

I believe the problem is you're using single quotes, where you need to use double quotes. A literal in single quotes is a single character, whereas a literal in double quotes is a string of characters.

I did that. Now the error it is giving me is the following:

'=' : cannot convert from 'const char[12]' to 'char[50]'.

Actually messed around with it and now this is how the line of code looks like:
product1[f].description = "";
Now the error it is giving me is: that the expression must be a modifiable lvalue.

EDIT: It's actually giving me both errors. The 'cannot convert' error and the 'lvalue' error for the same line of code.

Remember guys this isn't homework. Just problems im picking out from the book for practice.
http://sc2sig.com/s/us/1614629-1.png NEWSWRITER
Australia October 07 2010 19:17Posts: 8
What you have in your struct is a buffer of characters. On the line that errors what you are trying to do is copy the string "Claw Hammer" into that buffer. Unfortunately, the assignment operator (=) in C++ is not smart enough to do this.

What you should be using in C++ is the std::string class. It is fairly similar to the String class in Java. If you replace the char[50] with a std::string you'll be pretty much set.
Let's settle this like men. You. Me. Steppes Of War. Now.
United States Rostered Member for Proxiteam October 20 2010 15:47Posts: 619
Hey guys. I'm back. Simple question: having trouble converting 207.10 base 10 to base 16. I've tried looking for online guides but can't find anything except calculators. I got the integer part of converting to base 16...i just dont know how to convert .10 to base 16. Thanks in advance.
http://sc2sig.com/s/us/1614629-1.png NEWSWRITER

<< Previous Page 2 of 2

Reply You must be logged in to comment. (Sign In)