Here's a hint
g++ -W -Wall -ansi -pedantic -O2 foo.cpp
foo.cpp: In function ‘int main()’:
foo.cpp:20: warning: ‘ntemp’ is used uninitialized in this function
Fix the order of assignments to actually swap two variables.
Here's a hint
g++ -W -Wall -ansi -pedantic -O2 foo.cpp
foo.cpp: In function ‘int main()’:
foo.cpp:20: warning: ‘ntemp’ is used uninitialized in this function
Fix the order of assignments to actually swap two variables.
Ah, I see.
It's that new style of CSV file without any comma's in it :rolleyes:
Perhaps the fields are separated with a tab character instead.
> char matrix_points[6][8];
Perhaps
std::string matrix_points[6][8];
would be better for storing things.
Type man libcurl
at the command prompt.
Well it it was decimal, would you still have a problem with it?
It's the same deal, just use base-2 instead of base-10.
Maybe start with the program where each club sends the ball a fixed distance.
Or maybe start with the program which only has one club.
Break it down into manageable tasks, or simpler versions of the tasks, then refine the program as you go along.
You don't have to write the whole thing in one sitting to do that.
> 1010.1010
Well to the left of the radix point, it's 1, 2, 4, 8 (2^0, 2^1, 2^2, 2^3 etc)
To the right, it's 1/2, 1/4, 1/8, 1/16 (2^-1, 2^-2, 2^-3, 2^-4 etc)
All you need is a record of the total, and a record of the smallest.
When you're done, subtract the smallest from the total, and calculate the average.
No arrays necessary.
> char month[13]
Try
char *month[12]={"January","Febuary","March", "April", "May", "June", "July", "August",
"September", "October", "November", "December"};
> cout << month << " " << day << " " << year << "\n";
You also need to actually call the function as well, say printMonth( month );
> will someone point me the reasons why a call to malloc fails even if we have enough memory ?
My guess is you're still using crusty old TurboC which is limited to 640K no matter how many GB of memory you have on your pentium powered, XP hosted machine.
"Hello, yes I'd like to buy a Ferrari please"
"Certainly sir, will you be wanting to replace the engine with an elastic band?"
Well I think there are some redundant white space characters you can get rid of for total unreadability :rolleyes:
For example, please reformat this
student *select(student *k)
{ int a;
printf("\n enter chosen");
scanf("%d",&a);
student *t=null;
t=k;
while(k!=null){
if((k->gpa)>a)
printf("\n (%d %s %f ",k->id,k->name,k->gpa);}
k=k->link;
}
To look more like this
student *select(student *k) {
int a;
printf("\n enter chosen");
scanf("%d",&a);
student *t=null; /*!! slipping into C++ mode here */
t=k;
while(k!=null) {
if((k->gpa)>a) /*!! braces here would really make your meaning clear */
printf("\n (%d %s %f ",k->id,k->name,k->gpa);
} /*!! I think this brace is in the wrong place */
/*!! That is, you want to step the list INSIDE the loop */
k=k->link;
/*!! You're supposed to return a student* here */
/*!! but you just fall off the end of the function with nothing */
}
See, already I've spotting that one of your linked list routines will lock up simply by reformatting the code to indicate the flow of the program. And that's just one I picked at random.
This simple act of clearing up your code will no doubt throw up a lot of similar problems.
First off, there is little in your code which cannot be compiled with a C compiler, so the first question is, are you learning C or C++. I'm assuming that the .cpp extension to your filename is you just taking whatever the IDE prompts you with.
The random bits which need C++ I …
> How do you clean out the contents of an array after you take input?
The problem goes away (or more precisely, never arises) if you use fgets() to read all input, then use sscanf() / strcpy() / strxxx or whatever to extract information from the line returned by fgets().
By using scanf(), you upset the whole input stream, and have to resort to all sorts of bizarre ways of getting out of the hole you just dug for yourself - a hole which simply didn't need to be dug in the first place.
Use code tags.
Hasn't anybody figured out the magic of code tags in this place?
Post what you can achieve so far, so we don't explain stuff you know how to do already.
> But when I give 1000 for a,b,c
Do the math - 1000 * 1000 * 1000 * sizeof(double)
Do you have 8GB of memory?
> if(dp==NULL)
Should be outside the first for loop, not inside.
Also, what's with all the printf/scanf inside a C++ program?
> Why does the following program work correctly
There is a big difference between "produces expected output" and "works correctly". Your program is not correct, even though it seems to pass the "works for me" test.
When run with some diagnostic software, I get
$ valgrind ./a.out
==20047== Memcheck, a memory error detector for x86-linux.
==20047== Copyright (C) 2002-2005, and GNU GPL'd, by Julian Seward et al.
==20047== Using valgrind-2.4.0, a program supervision framework for x86-linux.
==20047== Copyright (C) 2000-2005, and GNU GPL'd, by Julian Seward et al.
==20047== For more details, rerun with: -v
==20047==
==20047== Invalid write of size 4
==20047== at 0x804838F: main (in ./a.out)
==20047== Address 0x52BFE7E4 is just below %esp. Possibly a bug in GCC/G++
==20047== v 2.96 or 3.0.X. To suppress, use: --workaround-gcc296-bugs=yes
20 30==20047==
==20047== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 13 from 1)
==20047== malloc/free: in use at exit: 0 bytes in 0 blocks.
==20047== malloc/free: 0 allocs, 0 frees, 0 bytes allocated.
==20047== For counts of detected errors, rerun with: -v
==20047== No malloc'd blocks -- no leaks are possible.
For your example, adding more code will no doubt show up all manner of weird problems, most likely close to a deadline.
> The problem I'm having is that when I use the function strstr() in a while loop, the console hangs
Well you don't advance the pointer, so it always begins the search from the same point, always finds the same match, and so on on on on .....
while ( (p=strstr(p,"thing")) != NULL ) {
// do something
// start search from just past the previous hit
p++;
}
> gets(array);
NEVER use gets() to read input, there is no way to make it safe.
Always use fgets().
> char string;
This should really be a local variable in some function.
Whether it's in main, and passed as a parameter to your function, or as a local within your function is up to you.
> while (*ptr != '\0')
This loop doesn't seem to append the \0 to the string you create.
> #include <iostream.h>
These are old-style headers.
New C++ compilers should support
#include <iostream>
using namespace std; // so we can do cout rather than std::cout
> #include <math.h>
The way to include a 'C' header in new C++ is to prefix the name of the header with a 'c', so
#include <cmath>
> void main()
main returns an int.
> a[j][count] -= (ratio * matrix[count]);
As far as I can tell, matrix is uninitialised at this point, so you just introduce garbage into your calculations.
Well that seems to be OK in itself.
Perhaps the problem is with the way you create the queue in the first place, and this is just where you notice that it doesn't work.
Post some more code please.
PS.
Use the CODE tags when posting code.
Perhaps you tried to use ^ as "raise to power" and got some very weird answers.
What you need to realise is that you need to call the pow() function in math.h, because ^ is the bitwise-xor operator.
> { if (schedule == true)
> { strcpy(schedule, "Booked");
The strcpy() makes no sense - schedule is an array of bool, not a string.
Does your compiler even warn you about this?
Or do you just ignore all warnings and just try it anyway?
Read this and edit your posts to include code tags
http://www.daniweb.com/techtalkforums/announcement8-3.html
> i got 2 errors n 1 warning on here so far after i compiled it
So post those as well. It's even better if you comment the code you post indicating which lines are causing you problems.
> void displayCalendar(unsigned month, unsigned year, int* daysOfMonth[])
The simplest rule is to simply copy/paste the declaration of the array you want to pass, so it would be void displayCalendar(unsigned month, unsigned year, int daysOfMonth[])
Calling the function, you just use the array name, so instead of displayCalendar(month, year, daysOfMonth[]);
It would be displayCalendar(month, year, daysOfMonth);
You appear to have missed this on your way in
http://www.daniweb.com/techtalkforums/announcement8-2.html
Better have a read of this on your way out.
http://www.catb.org/~esr/faqs/smart-questions.html#urgent
A quick stab in the dark would suggest you seek out the project web site / documentation / mailing lists.
> already searched the web, but no luck so far
So, is this a homework exercise in seeing who has the best google skills, or some attempt to weed out the lazy wannabe's (aka you) from someone far more interested and capable in programming?
If you're not even going to post an attempt (which we will gladly help you with), then why are you even bothering to take the course?
It's certainly an excellent example of not following the rules
http://www.daniweb.com/techtalkforums/announcement8-3.html
A lot of people just find other posts to read when presented with pages of unformatted code.
Looks like some tutor's dusted off an old assignment.
Recently bumped by a fellow student perhaps ? http://www.daniweb.com/techtalkforums/thread7832.html
There are essentially two steps to the problem, given
void foo ( int num );
1. do something with num % 2
2. call foo ( num / 2 );
Changing the order of those two steps changes what happens - feel free to experiment.
Oh, and you also need something to decide when to stop recursing otherwise you'll just go on until you run out of stack.
No way anyone is going to wade through that unless you edit it to include code tags
http://www.daniweb.com/techtalkforums/announcement8-3.html
> VERY VERY SUPER URGENT
http://www.catb.org/~esr/faqs/smart-questions.html#urgent
> I have tried but keep receiving errors
Of course, not posting the errors goes a long way :rolleyes:
You merely declare a tree and then read in some numbers. How about actually inserting those numbers into the tree before calling check()?
Do you even have a constructor for your tree to make it properly empty before trying to check() it? Is it for example just barfing on junk data?
Being able to write test code and debug your own code is a primary skill, so you may as well get some practice in.
Why don't you write some test programs for yourself to create trees with varying degrees of unbalance to test the function.
fgets() to read a line of input.
strtol() to validate, convert and check for numeric overflow.
Both provide some success/fail indication in their status returns.
> void main()
main returns int.
> while ( 0 == isOpen)
Cute trick.
Will you remember the == when you're comparing two variables?
The problem is, the compiler doesn't see your sneaky way of changing the variable, so it just assumes that isOpen is constant since you don't change it inside the loop. So it gets loaded ONCE when the loop enters for the first time (when it's probably still 0), and thereafter is is never read again.
To fix this, you need to indicate that the variable may change at any time, you do this with
volatile int isOpen = 0;
> op = (open_parameters*) malloc( sizeof ( open_parameters ) );
> op = (open_parameters*) open_para;
This is a memory leak, there was no need to malloc anything at all.
Also, since you're writing in C, the cast is unnecessary, so it's simply
op = open_para;
But I guess you're compiling your C code with a C++ compiler anyway, so I'll save you the trouble of moaning about the cast being necessary and just tell you to figure out how to compile for the language you're writing in.
> But, I didn't need to add a null pointer to the end of the string. It works anyways.
1. It's a NUL char, not a NULL pointer.
2. Yes, you do need to add it, otherwise you're relying on the buffer being initialised (which it isn't).
On the other hand, you could be sending the \0 (either by accident or by design).
Wanna see?
Try
memset( buffer, 0xff, sizeof(buffer); /* very specific non-zero fill */
sr = recv(acc, buffer, sizeof(buffer)-1, 0);
cout << "sr = " << sr << "\n";
buffStr = buffer;
Now see whether buffStr is correct or not?
> The output of sr is 1, which is not the size of the buffer
Who said it would be?
The size you pass is "don't go past this many characters", not "please hang around until you fill the buffer".
You got 1 character, so copy it to somewhere else, and call recv() again.
Keep calling it until you have all your data.
> int sr = recv(acc, buffer, sizeof(buffer), 0);
1. recv doesn't store a \0 on the end to make it a proper string
2. You need to leave room to store the \0 yourself
3. You need to code for the possibility that recv() doesn't receive the whole message in one go (if this is a TCP connection)
int sr = recv(acc, buffer, sizeof(buffer)-1, 0);
if ( sr > 0 ) {
buffer[sr] = '\0'; /* make it a string */
/* now you can do str... operations on buff */
}
> strcpy(sendBuffer, temp.c_str());
If you're going to do this, why are you bothering with 'C' strings at all?
Perhaps it's more to do with entering more than 80 characters rather than the number of words.
How does getline for example know how many characters to input, and not overflow the buffer?
According to my info, the 2nd parameter is a length, not a char to stop at.
I'd suggest you look around say http://www.avrfreaks.net/ if you want to get into AVR programming.
ARM processors are used in a number of mobile phones, I've no idea about that specific one though.
> urgently needed as submissions by next week...
What makes you special?
http://www.daniweb.com/techtalkforums/announcement8-2.html
Noting also that this was 9 months old before you said "me too".
> cin >> inp[40];
The next problem is you're inputting only a single char, but its off the end of your array.
cin >> inp;
Would input a single word into your array.
> inp[k] = '\n';
This loop does nothing useful.
> if (trade(word == '\n'))
This performs a comparison, and then passes the boolean result 0 or 1 to the trade function.
Is this what you wanted?
> y = y>>8;
Right shift of negative numbers is implementation defined.
On some systems, the sign bit is propagated
11110000 would become 11111000
and on others zero is inserted.
11110000 would become 01111000
Well did you open the file for output in binary mode?
Did you check the sizes of the file on the two different systems was the same?
Have you copied a file generated on your Linux box to the windows box, and tried to play that?
http://cch.loria.fr/documentation/IEEE754/ACM/goldberg.pdf
Read this and understand how all floats are approximations, which means no matter what you do, you invariably end up with ab.cd0000001 or ab.cc999999 type numbers as being the nearest representable value to ab.cd
Using floats for storing money is a really bad idea.