![]() |
C Programming Homework Help Needed
1 Attachment(s)
Hi all,
I'm taking an course, "Structured Programming in C" and am having some issues with my homework for the week. My assignment is to write a program that calculates various aspects of projectile motion, but I am running into a problem with an output(?) in my program. When I compile the program I receive no errors or warnings, but when I try to enter values into the first input, it turns the value into '+NAN'. I don't know what '+NAN' is or how to fix it. I've attached a screen capture of what happens when the program is run and values are entered. HELP! Please excuse the messiness of the code, as it is not completed or functional. Code:
/* |
Re: C Programming Homework Help Needed
As one step, you can remove the & sign from your printf statements.
For example, printf("Time of flight is %f seconds.\n", &time); becomes: printf("Time of flight is %f seconds.\n", time); |
Re: C Programming Homework Help Needed
Joel is correct. Remove the & from your printf functions. The & means "address of". Printf doesn't want the address of the location where the number is kept, printf wants the number itself.
FYI, the & is correct in the scanf function. Scanf requires the address of the variable. Btw NAN means "Not A Number". Flash. |
Re: C Programming Homework Help Needed
I'm a bit confused as to exactly how this code compiled, and why it isn't happy. I know exactly enough C to confuse myself.
I understand why the code is wrong. I don't fully understand why this is the error that is given. Since printf takes a variable number of untyped objects, I'll believe that it can't catch the type mismatch of address and float. This must be the case, since it did compile. Since C has no run-time environment to speak of, we must assume that the computer will do exactly what it is told: Take the address and interpret it as a float. The code then loosely translates to: Code:
float time;I may be reading too far into this, but I'd really appreciate the assistance of someone with a deeper understanding of C. Thanks Eric |
Re: C Programming Homework Help Needed
I just ran it in Code::Blocks (open-source IDE, it is quite good), and no matter what number I entered, I got 0.0000 degrees, 0.00000 radians.
Check the scanf documentation, there is probably something small you're doing wrong. Edit: I read the scanf documentation, and there doesn't appear to be anything wrong. Good luck. |
Re: C Programming Homework Help Needed
1 Attachment(s)
Quote:
To answer another question, I'm using the Borland C 5.5 compiler. |
Re: C Programming Homework Help Needed
2 Attachment(s)
Back again...
I'm only concerned with a small portion of my program at the moment. This part collects an integer value for use to set the color of an object later in the program, then checks to make sure it is within a certain range of numbers. If the number isn't within that set of numbers, it is supposed to ask the user to enter again, then collects a value again. Currently, whether the value entered is within the parameters that I want, this chunk of program gets stuck in the error checking loop. Oddly, when you enter a two digit integer, it breaks the loop. Code:
/* Collects shape selection from user and checks for invalid inputs */ |
Re: C Programming Homework Help Needed
Pat,
It looks like you are not being consistant with the data type for "color". You are inputting it as an integer (%d) so I'll assume you've defined it as such, however you are comparing it in the switch statement as a character. The value '0' will be converted to it's ASCII value (48) for the compare. If 'color' is an integer, then I'd try removing the single quotes from around the numbers in the case statements and see if that works. Mike |
Re: C Programming Homework Help Needed
Quote:
|
Re: C Programming Homework Help Needed
I think I need to make this the "Help Pat pass EGR 261" thread. :p
My problem this time is that a switch statement again. After the menu displays, the user can enter a selection, but when they hit enter, it just displays a blank line and doesn't leave the switch statement. Thanks for the continuing help guys.:) Code:
int menu_error; |
Re: C Programming Homework Help Needed
Pat,
I'm not sure the problem is in the switch statement. Try removing the \n from the menu_selection scanf line ("%d" rather than "%d\n") Mike |
Re: C Programming Homework Help Needed
Quote:
|
Re: C Programming Homework Help Needed
Back again!
I can't figure out how to convert a float value to a char string. Suggestions? Thanks! |
Re: C Programming Homework Help Needed
Quote:
Idk how you would get each number individually but to change the number into its ASCII code you would do # + '0' to get the ascii code of the number |
Re: C Programming Homework Help Needed
Quote:
|
Re: C Programming Homework Help Needed
The function you are looking for is called "sprintf".
Use it just like you would use printf to print a float. The trick is that it throws it into a string, rather than at the console. Code:
char * buffer = new char[10]; |
Re: C Programming Homework Help Needed
Is there a header file you need to include?
I'm getting a 'no prototype for function' error for the sprintf function. |
Re: C Programming Homework Help Needed
Quote:
|
Re: C Programming Homework Help Needed
Quote:
The first line: char * buffer = new char[10]; is confusing me. |
Re: C Programming Homework Help Needed
Quote:
char buffer[10]; /* bigger than 10 is a good idea */ If a static or automatic array (depending on the location of the declaration) is good enough, or char *buffer; buffer = malloc(10); /* again, bigger than 10 is a good idea */ to allocate the storage using the heap allocator in C. Eugene |
Re: C Programming Homework Help Needed
1 Attachment(s)
I am using this code:
Code:
#include <stdio.h> |
Re: C Programming Homework Help Needed
Quote:
|
Re: C Programming Homework Help Needed
Quote:
Now how can I use the char value? I tried: printf("%c", buffer); But that printed out an an arrow pointing up.:p |
Re: C Programming Homework Help Needed
Quote:
printf("%s", buffer); printf("%c", buffer[0]); would print the first char Eugene |
Re: C Programming Homework Help Needed
Quote:
|
Re: C Programming Homework Help Needed
Quote:
Code:
float f = 12.4;If you ever have a simple question about a function, Google is your friend. (Just put in the function name or "C functionName".) Even Wikipedia has an article about most standard functions. |
Re: C Programming Homework Help Needed
1 Attachment(s)
This code is supposed to convert a base 10 number, 123, into binary and display the value. Somewhere in the conversion, the division loop stops before it should, for a reason that I cannot identify.
Output with some diagnostic print statements attached. Code:
/* Patrick McCarthy */ |
Re: C Programming Homework Help Needed
A few notes to make your life easier:
The switch(base) structure is confusing. I suggest replacing it with something that more clearly states what you want. I'd recommend something like Code:
if(base <2 || base > 10) { Error !!}Your printf statement doesn't really tell the user what you are going to do with the number. Put a more informative request in there. "Please enter the base of the number (between 2 and 10)" Please comment what q and r are. They are probably quotient and remainder, but explicit = maintainable. Okay, enough with the picky details. Pain in the patookus, but trust me, you'll thank me later. To clarify divide, I've used pass by reference, rather than by pointer. Also, I've added some more debuggging! Code:
void divide(int D, int N, int &q, int &r)_____________________________________ Thought of a few more helper functions. Code:
int convertFromBaseToBinary(int base, char * number_string)__________________________ Just noticed another bug. for(j = 1; q[j] >= 0; j++) will never terminate. I think you want q[j]>0 or maybe even q[j-1]>0 Edit 3 _________________________ And for more giggles, here is some Python Code:
def convertToBase(num, base): |
Re: C Programming Homework Help Needed
1 Attachment(s)
I'm having a little bit of trouble now. See output.
Code:
/* Patrick McCarthy */ |
Re: C Programming Homework Help Needed
You only half integrated my changes to divide.
q and r are coming in as references. You do not need to declare them again. Also, divide(base, q[j-1], &q[j], &r[j]); should read as divide(base, q[j-1], q[j], r[j]); I'm going to be offline for a while. Good luck! |
Re: C Programming Homework Help Needed
Got it working, thanks!
|
Re: C Programming Homework Help Needed
1 Attachment(s)
Don't you just hate those programs that have such a simple concept behind them, but are incredibly difficult to come up with a logarithm for?
I've got the embedded loops working properly, they start with the first character of the string, then check against all the following characters, then advances to the second character and checked against all the following characters and so forth. I just can't seem to figure out the proper way to record if a character has been used, then make the loop exclude that character. I could do it with some brute force tactics, but I'm sure there is a more elegant and less time consuming way. Code:
/* Author: Patrick McCarthy |
Re: C Programming Homework Help Needed
Quote:
|
Re: C Programming Homework Help Needed
Quote:
Code:
int ascii_flag[128]; |
Re: C Programming Homework Help Needed
Quote:
Code:
for(i=0;i<128;i++){ |
Re: C Programming Homework Help Needed
Quote:
|
Re: C Programming Homework Help Needed
1 Attachment(s)
Ok, I think I'm really close, but there are a few problems still.
The program is supposed to not let letters of the alphabet be used more than once, whether that letter is upper or lower case. However, if the character is anything other than a letter, it is supposed to go right through the system, regardless of repeats. Problems: Currently, if a capital letter is the first character of the string, it won't print, but the same letter will print again after. The last letter seems to be truncated. After I get those problems worked out, I think that I can uncomment the last else if to take care of the remaining characters properly. Thanks Code:
/* Author: Patrick McCarthy |
Re: C Programming Homework Help Needed
Bump, because I'm still stuck.
|
Re: C Programming Homework Help Needed
I don't understand the primary/secondary looping you're doing. You shouldn't have to look at each character more than once as you step through the string. If it's a non-letter, print it. If it's a letter and the character's flag isn't set to say you have seen it before, print it, and set that character's flag. It should be that simple.
|
Re: C Programming Homework Help Needed
Quote:
Still working on it. |
Re: C Programming Homework Help Needed
I've cleaned it up a bit, but it quits reading the string after a space.
Also, I've identified a few problem characters: d, w, z, Z Those characters give ☺ as a screen output. Any clue as to why it would print the 'alt + 1' character instead of those letters? Code:
/* Author: Patrick McCarthy |
Re: C Programming Homework Help Needed
I'm having difficulty reading through the wordiness. Changing it to be a little more compact might help reveal any possible logic or syntax errors. For example, your code refers to temp[primary] a LOT. It might be clearer to say thischar = temp[primary]; once at the beginning of the loop, then refer to thischar thereafter.
Also, now that you've removed the "inner" loop, the term "primary" no longer applies. As part of the cleanup, consider changing it to "cursor" or something similar. I'm not sure what the name "ascii_flag" is supposed to mean; I'd have called it "letter_seen" or "already_printed" or "filter" or something descriptive. I do see a couple of things that probably aren't right. The outer if statement looks either redundant or outright wrong. What is it supposed to be doing? The lowercase section looks at the flag for uppercase letters, and vice versa; you probably shouldn't be adding or subtracting the 32 when checking the flag. There are also a few spots where I'd have combined operations, such as turning the two lines of printing at the end into a single printf() or combining the separate final[j] = ...; and j++; into a single final[j++] = ...;, but that's arguably a matter of personal style. One last thing: I don't see anywhere that puts the trailing /0 on the string you're building in final. Without that string terminator, you'll have problems printing it. |
Re: C Programming Homework Help Needed
If you only care about a..z and A..Z but only need 1 flag per lower/upper pair that means just 26 flags. I'd have used a simple mask in a unsigned long instead of an array.
Initializing the single bit mask variable is straight forward. Then leverage the use of standard string/character functions [often implemented as macros]. If the character isalpha() [true if a..z or A..Z], then use tolower() to force the character to lower case. Subtract 'a' from this to get a 0..25 offset. Use the offset to create the bit check mask to see if the letter has been used before. If yes, then just continue on in the for loop. If no, set the bit to show the letter has been used and drop into common code that accumulates the character into the output string (and prints out the character). Repeat. Something like: Code:
#include <string.h>Code:
if( ((97 <= (temp[primary])) && ((temp[primary]) >= 122)) == 1)if 'a' <= current input char and current input char >= 'z' So }, |, {, ~ will meet this test because the character has to be greater than 'z' and all such characters will also meet the criteria that 'a' be less than that character. I think you meant to have if current input char >= 'a' and current input char <= 'z' then process a..z ? Bud |
Re: C Programming Homework Help Needed
Thanks for the input guys, I've got it working finally.
Code:
/* Author: Patrick McCarthy |
Re: C Programming Homework Help Needed
Hi, looking for some help again!
This portion of my code prompts the user to input a 4-bit base-3 number then is supposed to check to make sure that all of the digits are within [0,2]. Currently, the error checking portion doesn't work properly. Many 4-bit numbers will work, many won't. I have a feeling it has to do with the array. Code:
void ThreeToNine(void) |
Re: C Programming Homework Help Needed
Hi Pat,
Shouldn't your "for" loop start with its index set to 2 rather than 3? Your array size is 3 -- with an index of 0, 1 or 2. Dave D |
Re: C Programming Homework Help Needed
I just fixed that, but I'm still having the same problem.
As an example, I'll enter 1111 and the error checking appears to work, but then I'll enter 1211 and it will say there is an error with the second digit. This happens for any digit that has a value of 2. |
Re: C Programming Homework Help Needed
Sorry, no, your index is OK, but your array declaration should read
int num[4]; not int num[3]; |
Re: C Programming Homework Help Needed
Thanks Dave! That seems to have taken care of it!
It's been a year since I've written code, so I'm a bit rusty. :p |
Re: C Programming Homework Help Needed
That C structured programming class seems to be dragging out for quite some time. It might be about time to start in on C++.
:) |
Re: C Programming Homework Help Needed
Quote:
|
C Programming Homework Help Needed
help on palindrome
i have make a program that can trace palindrome number but i need it to loop over and over again till i keyin -1 then only the program terminate this is the code that i have used: Code:
#include<stdio.h> |
Re: C Programming Homework Help Needed
Quote:
You want it to end? Hint: use an exit statement, but only execute it when the program is supposed to terminate. |
Re: C Programming Homework Help Needed
What exactly do you mean by "loop"? Do you want it to repeatedly ask for a number, then check it for palindrome-ness (sorry, vocabulary suffered a non-Build season hit :D ) , or do you want to loop through calculations (a bit daft, but I don't know what the professor/employer expects...), or what?
|
| All times are GMT -5. The time now is 13:22. |
Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi