The code is from the professor, so i assume she is right :P
In my classes, I never assume the professor gave good data. I assume they will throw in bad data to see if we can handle it. It's your call.
The code is from the professor, so i assume she is right :P
In my classes, I never assume the professor gave good data. I assume they will throw in bad data to see if we can handle it. It's your call.
Did you split the functionality, or are you going to insist on doing the two things together?
Not quite. After you input all the numbers, set min to arraySize[0].
Then you code should work fine.
Why are you using command line parameters (argc/argv
) for this?
Are you guaranteed that the file has the correct data so you don't need much error checking? If so, you can use fscanf()
to read the values.
If not, use fgets()
to read a line and check if the correct characters are there before converting to integers.
Depends. What issues? They aren't secret issues, are they?
It's hard to do two things at once.
Try doing the sort first.
Then look for the duplicates.
Card[nCards]
is one beyond the array definition. Since you allocated nCards integers, your last available value is Card[nCards-1]
.
Things can get confusing sometimes with pointers and addresses.
That's for sure.
What can help is drawing a map of the pointers on paper to see if you're using the correct syntax.
I don't understand the term "some sort of error". Post the error. Don't take a picture of it and link.
Also, I noticed that it didn't add quotations and curly braces automatically when I created the first one.
The .cpp file was completely empty so I had to add the headers in myself.
Are these problems? For most of us the IDE doesn't write the code for us. That's the programmer's job.
My first thought when I saw "...I had to add the headers in myself" was "poor baby"... ;o)
I've been adding my own headers for 30 years.
Much better.... ;o)
What I meant was "make a function that authenticates user input. Make it such that it checks for all types of inputs, like midterm marks, lab marks, quiz marks etc. is overkill-- at least if each are graded differently. Since the loop seems to be the learning feature, functions will help see the loop structure better by taking the clutter out of the loop. Cleaner as you say.
But it might also detract by adding another layer of learning over and above the preceived lesson if functions have not been introduced. Since we don't know what the OP has learned yet, we don't know if functions are available. I'm not saying it's a bad suggestion, though...
Where are you dividing?
Just before those statements, output the values used to figure out the denominator. For example, for line 43, display i and a[i][i]
Now figure out what that value should be and why it is zero.
It's because node->data = data;
stores the address of your variable input into the node during the first pass through the loop.
Next time through the loop you enter a new word into input. Since all you stored is the address of input and not the data itself, the data is now changed.
IOW, in each node, the data element points to the exact same buffer, therefore whatever is stored in the buffer input at the end of the loop is what each node points to.
Key statement above: "all you stored is the address of input and not the data itself".
For Example:
char *c; c="END"; c="BEGIN"; No errors
"END" and "BEGIN" are by definition allocated sequential memory during compile time. Then c="END";
simply loads the address where "END" is defined into the pointer c. Same with c="BEGIN";
. It's simply loading a new address into the pointer.
while int *d; d={1,2,3,4,5}; Gives an error
In this case you are attempting to load 1 thru 5 which are not allocated in sequential memory but are simply values and this syntax does not equate to a memory address.
Further:
"END" is defined as the sequence 'E','N','D','\0'
by the compiler. But, where c="END";
is valid, c={'E','N','D','\0'};
which looks like it should be the same but is not, for the same reason above.
in fact to use d, an allocation of memory has to be done and then filling the array must to be through filling element by element while using c is so simple!!
AND ALSO, one an array of integers is initialized can't be modified except by accessing its element. But as in the above example string c is modified with no problem
As I mentioned, modifying the string is done by loading another string address into the pointer. The integers are not pointed to by a pointer.
To replicate the char *c; c="END"; c="BEGIN";
concept with integers would be:
char *int1 = {1,2,3,4,5}; // this would replicate "END"
char *int2 = {9,8,7,6,5}; // this replicates "BEGIN"
char *c = int1; // this is …
If there's enough memory to be given why would a segmentation fault ocuur ,
If you ask for 20 bytes for the array prices and tried to access prices[20], you just tried to access memory you don't own. You only own up to prices[19]
also about writting your own malloc you can do it , it won't be that easy but doable .
Probably, if you understand how the operating system does things and have access and permissions to the proper O/S calls.
hey this works, but it isnt working...
What? How can it work but not work?
like if i input less than 0 and greater than 20,
OK, you're testing your limits as the instructions say you need...
it generates, retype mid term marks,
Generates? You mean it outputs? Or something else? If you mean outputs, that's what your instructions said you need.
and if i input betweeen 0-20 it proceeds
And that's what the instructions say it should do, isn't it? So there's not a problem, right?
I guess I don't understand your post...
I've used malloc countless times, but as far as I know, reading and writing to memory that isn't expressly yours (which is in fact done by the malloc function) causes a segfault... So why doesn't malloc generate segfaults?
For the same reason when you fly to Pheonix and rent a car the cops don't arrest you for driving a stolen vehicle. It is yours for the duration of the rental. So the memory malloc()
gets is yours -- you basically rented it until either the program ends or you call free()
...it sounds like you're questioning how malloc() works.
I read it more as why malloc()
works... ;o)
And I learned something - another C confusion factor uncovered. Why are || and && sequence points? Rhetorical, no need to explain - I can see why they could be just not why they should be...
What I would do in this case is print out values the program calculates at key places and see what the variables contain. For example
After line 29 output computerguess
After line 41, 76, 85 output computerGuess, lowLimit, highLimit
An example, after 76:cout << "In setLimits (high)" << computerGuess << lowLimit << highLimit << endl;
Also, you can delete lines 35 thru 43 by moving 41 after line 31. In place of line 29, set lowLimit, highLimit to appropriate values.
Start by writing a piece of code with two pointers and known contents.
Output the value of the pointers and the data that they point to.
Now, do both increments on the two pointers.
Next, output the value of the pointers and the data that they point to again.
Last, compare the outputs.
Oops. Sorry, my mistake. It isn't undefined.
Well it's my guess that lazy students aren't facebook friends with their instructors :)
Well, that's true. Not that they can't find their Twitter account easily.
If you can't wrap your mind around which types of threads would be socialized upon being posted, here is just one of many recent examples
Oh, I actually can come up with types of threads that are socializable. I kinda doubt the forums I haunt would have many likely candidates. But some do exist.
Maybe a code snippet or two - if I was brave ;o)
Maybe how-to posts like some of the stickys
Those types of things in the Software Forums.
In this thread I'm playing more of a devil's advocate, egging them on.
Hmmm, I guess my cover is blown.
Omelettes, anyone?
The publicity team for Technozion is in full swing. Like every year, this time too, the team is planning to visit N different colleges in India. This publicity campaign shall involve the members of the team giving presentations...
How about presentations on
"The Benefits of Using 25-year-old technology (TurboC) and Handcuffing the Students to (equivalently) Bronze Age Technology".
"How the Word 'Question' Becomes Twisted into the Word 'Doubt'"
"How to Make Yourself Understood When They Call Your American Tech Help Desk"
But I kid... ;o)
You forgot the part about it being undefined behavior...
;o)
WaltP was negative about the fact that I have used main(); and about the fact that my program will crash if you enter a character instead of a value.
Not just me. Others pointed it out first and I agreed. And you argued that your program was perfect. When asked "what happens when you input a character?" you responded:
You: The program will crash. You have to fill in a number.
You: I have tested my program already. I solved every single mistake which I have made. The program also takes to account with leap years and with correctionyears.
You: It is a perfect program now. :-D
You: It is. The program will prevent you from entering invalid data.
You: When you enter characters instead of numbers, the program doesn't know what to do with it, so it crashes.
Crashing is an error, not a valid way to prevent invalid data
See the pattern?
You: In some if-statements I am using main(); to restart the program. That is not bad!
More arguing about a bad practice.
I asked WaltP which code I have to use to restart my program is main(); is not good enough, and which code I have to use so my program won't crash if you enter a character instead of a value.
And why should I wish to help after all the above attitude, arguing, telling us we are wrong? What makes you think …
Another idea for finding the min and max grade:
Input midtermgrade
set Min and Max to this grade (one of them *could* be true)
input projectaverage
test with Min & Max and replace if necessary
input papergrade
test with Min & Max and replace if necessary
This way you won't need the confusing IF statements you currently have.
First things first. This is C++ forum not C.
So? Isn't C++ based on C? Therefore C code is valid.
Now your question.
You can make a function that authenticates user input. Make it such that it checks for all types of inputs, like midterm marks, lab marks, quiz marks etc.
True, but overkill for his current purposes -- learning to simply make a loop.
This should work fine. Try implementing it.
Terrible! Why use an endless while
loop and break
out of it when while (flag)
is designed to do it without the break?
No, I'm not talking about the need to ask. It's the idea of asking a question like "Gimme the code for flogging deceased equines. I don't know how" and then tweeting it. Especially when your instructor can find it easier.
Adding this feature alone has increased our "social activity" from an average of 250 likes/tweets per day to forum discussion threads up to an average of over 400 per day.
WOW!!! Significant! And surprising, at least to me. With many of the posts I see, I'd be embarrassed to socialize them. Are these likes/tweets across the forums or mostly in a certain cross-section of the forums? What percentage of the new threads are socialized from the message in question? What percentage from other directions?
For example, we just fairly recently integrated a Login with Facebook feature, and we're now seeing an average of 500-600 new members registering through Facebook daily! What this means is that we first created the Login with Facebook feature late July/early August, and we already have seen over 20,000 new members joining our community thanks to the feature.
This one actually, if I thought about it, makes sense. That never bothered me, though.
While I can appreciate that you guys might find it a bit irritating, I also need to take into consideration that most regulars very rarely start new threads, and therefore this is not a feature that needs to appeal to regulars as much as the newbies who will really take advantage of this feature.
Absolutely true...
For example, you, WaltP, have been a member for over 6 years and have only ever started 87 threads.
And mostly in Feedback and Mod I bet...
No. It needs to be in the project directory -- where the .EXE lives.
Using puts()
-- BRAVO!!! Finally someone who doesn't use printf()
:o)
Using main()
-- Oops. main()
is an int. Always.
Nope. Never cared to progam anything like that.
I wonder which compiler you are using, because my compiler compiles it without any problem. My computer, Windows 2000, is also running it without having any problems.
Well, my compiler and my computer won't, therefore it's not a good program. A good program should compile with any standard C++ compiler, on any system.
In some if-statements I am using main(); to restart the program. That is not bad!
Who says? You? Or the people that designed C++? I'm going to listen to the designers, thank you very much. They say it's bad.
My program doesn't have mistakes, because I already solved them. :-D
As decepticon said...
How long have you been programming?
Especially if you are a (shudder) student.
... and was just wondering if its possible to make a simple program where you type the name of the file and when you press enter it downloads that file from the internet to a certain place on your computer.
Of course it is.
where would the files be stored?
Where your program tells it, or better yet, where the user says.
could you store on internet or would they have to come from my pc via internet to were i want it downloaded?
Either.
could you show some source code with explanations of it?
No. But you can search the internet for information and example code.
im very new to c++
Then you aren't ready to tackle this progrm yet. You need to learn a lot more about the language before attempting to try networking.
If you are beginner, then you should heed the advice you are being given.
If you believe that it is OK to have your program crash with a simple
incorrect entry then the coding path ahead is going to be long and hard.
Sorry, but she's absolutely correct.
What would you do if your favorite game crashed because you accidentally entered "rt" instead of "45"? Learn from the advice. Good students don't argue, they listen.
hey, i tried a lot, i made it all
I see no code to show us what you tried so therefore cannot help you fix it.
Unless, of course, you want us to write it for you...
Now I see what's happening. It happens when you start a thread!
For every lame-assed question we post to get help, we're being asked to broadcast our stupidity on Twitter and Facebook. Why the 7734 would we want to do that????
I agree. Get rid of it -- it's annoying and useless.
It is quite fundamentally flawed
Agreed. And the formatting is terrible.
Calling system()
is not necessary and not portable. When compiling on my Linux system the program doesn't work as advertised. On my Windows system I get nauseous with all the blinking screens.
Too many convoluted IF statements where SWITCH statements would be cleaner.
Calling main()
is not OK. main()
is the entry point to the program, not a recursive function. Very bad!!!
I have tested my program already. I solved every single mistake which I have made.
Obviously not.
That's possible. Its also possible he's trying to find the stuff between the tags. Or even extracting the stuff after the closing tags.
I'm now done guessing and awaiting a clear example of what he wants. From him.
Another ambiguous post... Maybe this will help:
[boilerplate_help_info]
Posting requests for help must be well thought out if you want help quickly and correctly. Your post did not meet the criteria for quality help. You may get some posts, but are they going to be useful? Check your post with these checkpoints - what is it you missed:
I can't tell if you simply made an explanation of what you are going to do or there's some unasked question you need help with.
Please be clear when you post. Both of the posts in this thread are ambiguous at best.
No, it's your naive algorithm. You go throught the comparisons a maximum of (N-1)^2 [361] times
If you stop comparing the values that are already in place, you can cut the comparisons to the summation(0-19) [190] times.
I now see where you got your initial algorithm. It's in Wikipedia along with a couple enhancements.
Ahh, we're playing Jeopardy. OK.
The question is: How can I read the file and store student information?
Did I win?
-------------------
In answer to your non-question, looks like either idea would work...
I tried the code you pointed to. It's the same as yours. It's kind of a bad algorithm.
Here's mine:
compare = 0
loop from j = 0 to N-2 // for (j=0; j<N-1; j++)
{
swaps = 0;
loop from i = 1 to N-j-1
increment compare
test if Array(i-1) > Array(i)
if so, swap
increment swaps
if swaps is 0 exit loop j
}
compare will now contain the number of compares made.
512 in binary is 10 0000 0000
The int is probably 2 bytes (guessing by your name you're using Turbo C)
So i contains (0000 0010)(0000 0000)
typecasting i to the character c, c[0] is (0000 0000), c[1] is (0000 0010)
Replace c[0] with (0000 0010) makes i (0000 0010)(0000 0010) which is 514.
Try outputting c[0] and c[1] and you'd be able to see what is happening. That is if you can do the binary conversion.
where did he say he doesnt want to use the non standard libraries?
He didn't.
We prefer to suggest standard functionality rather than relying on tricks and non-standard techniques because you cannot transfer the non-standard skills to other compilers. Especially things like getch()
.
Also, if he's using g++, Watcom, or any of a couple hundred other compilers, using getch()
isn't even an option. So we keep our help standard unless specifically asked.
he even used it in his code just in the wrong place :O
though i could be wrong >.<
Yes, you are wrong. His code is standard.
Standard C++ does not allow this. Only a couple compilers have extensions that will, but learning to rely on non-standard functionality hurts you when you need to use different compilers.
Can C pass by reference?
Yes, with a slightly different syntax.
Is there something about structs C doesn't like because everywhere my sruct is used it gives an error.
Generally, no. Since I can't see your errors from here, it's hard to tell what the problem is.
Does C have 'for' loops?
You're kidding, right?
I created a new node:
node = new animalTree(data);with a constructor that initializes data and the left right nodes.
how do I do that in C
node = (animalTree(data)*) malloc(sizeof(animalTree)); ???'
For the most part. Since you did't post any real code, it's hard to tell. There are no 'constructors', you define structures and they are 'constructed' when you assign a variable name to them.
If there was a "BEST", there's be only one. We wouldn't need the rest because they'd all be crap.