![]() |
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) |
| 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