Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Programming (http://www.chiefdelphi.com/forums/forumdisplay.php?f=51)
-   -   C Programming Homework Help Needed (http://www.chiefdelphi.com/forums/showthread.php?t=57552)

Pat McCarthy 02-09-2007 21:12

Re: C Programming Homework Help Needed
 
Got it working, thanks!

Pat McCarthy 08-09-2007 13:51

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
 * Filename:        partiv.c
 * Description:        Takes in a string and prints it out with no character used more than once.
*/

#include <stdio.h>
#include <string.h>

int main(void);

int main(void)
{
        char temp[19];
        char final[19];
        int primary, secondary, j=0;
        int length;
        int used = 0, used_num;

        printf("Please enter a word or phrase => ");
        gets(temp);

        length = strlen(temp);

        /* Primary loop:
        * Starts with first string element, checks against all following
        * string elements for repeats
        */
        for(primary = 0; primary < length; primary++)
        {
                //printf("Primary = %d\n",primary);
               
                /* Secondary loop */
                for(secondary = primary + 1; secondary < length; secondary++)
                {
                        //printf("Secondary = %d\n",secondary);
                       
                        switch(used)
                        {
                                case 0:
                                {
                                        if(temp[primary] != temp[secondary])
                                        {
                                                final[j] = temp[primary];
                                                j++;
                                                used = 1;
                                        }
                                        break;
                                }
                                case 1:
                                {
                                        break;
                                }
                        }
                        /* Resets variable used to an unused condition */
                        used = 0;
                }

        }

        printf("\n");
        puts(final);
       
        return 0;
}


Alan Anderson 08-09-2007 15:00

Re: C Programming Homework Help Needed
 
Quote:

Originally Posted by Pat McCarthy (Post 641355)
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.

The way I'd do it is to have an array of flags, indexed by the ASCII value of the character. Before starting to print the string, set all flags to FALSE. For each character in the string, if the flag for that character's value is already TRUE, skip the character. Otherwise, print the character and set the flag to TRUE.

Pat McCarthy 08-09-2007 15:17

Re: C Programming Homework Help Needed
 
Quote:

Originally Posted by Alan Anderson (Post 641357)
The way I'd do it is to have an array of flags, indexed by the ASCII value of the character. Before starting to print the string, set all flags to FALSE.

When you declare an array like this:
Code:

int ascii_flag[128];
Does that automatically assign 0 or FALSE to each variable in the array?

DanDon 08-09-2007 15:51

Re: C Programming Homework Help Needed
 
Quote:

Originally Posted by Pat McCarthy (Post 641359)
When you declare an array like this:
Code:

int ascii_flag[128];
Does that automatically assign 0 or FALSE to each variable in the array?

I would personally run through a loop to make sure that each member of the array is initialized to FALSE.

Code:

for(i=0;i<128;i++){
    ascii_flag[i]=0;
}


Pat McCarthy 08-09-2007 16:38

Re: C Programming Homework Help Needed
 
Quote:

Originally Posted by dhoizner (Post 641364)
I would personally run through a loop to make sure that each member of the array is initialized to FALSE.

Code:

for(i=0;i<128;i++){
    ascii_flag[i]=0;
}


Thanks, that helped!

Pat McCarthy 09-09-2007 13:43

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
 * Filename:        partiv.c
 * Description:        Takes in a string and prints it out with no letter of the alphabet used more than once.
*/

#include <stdio.h>
#include <string.h>

int main(void);

int main(void)
{
        char temp[19];
        int i;
        char final[19];
        int primary, secondary, j=0;
        int length;
        int ascii_flag[128];

        printf("Please enter a word or phrase => ");
        fflush(stdin);
        gets(temp);

        length = strlen(temp);

        for(i=0;i<128;i++)
        {
                ascii_flag[i]=0;
        }
       
       
        /* Primary loop:
        * Starts with first string element, checks against all following
        * string elements for repeats
        */
        for(primary = 0; primary < length; primary++)
        {
        //        printf("Primary = %d\n",primary);
               
                /* Secondary loop */
                for(secondary = primary + 1; secondary < length; secondary++)
                {
                //        printf("Secondary = %d\n",secondary);
                       
                        if(ascii_flag[temp[primary]] == 0)
                        {
                                // Checks for characters that are lowercase or uppercase letters
                                if( ((97 <= (temp[primary])) && ((temp[primary]) >= 122)) == 1)
                                {
                                        if(ascii_flag[temp[primary]-32] != 0)
                                        {
                                                final[j] = temp[primary];
                                                j++;
                                                ascii_flag[temp[primary]] = 1;
                                                ascii_flag[temp[primary]-32] = 1;
                                        }
                                }
                                else if( (((65 <= (temp[primary])) && ((temp[primary]) >= 90))) == 1)
                                {
                                        if(ascii_flag[temp[primary]+32] != 0)
                                        {
                                                final[j] = temp[primary];
                                                j++;
                                                ascii_flag[temp[primary]] = 1;
                                                ascii_flag[temp[primary]+32] = 1;
                                        }
                                }
                                // Processes the characters other than letters
                        /*        else if(ascii_flag[temp[primary]] == 0)
                                {
                                        final[j] = temp[primary];
                                        j++;
                                }*/
                        }
                }

        }

        printf("\n");
        puts(final);
       
        return 0;
}


Pat McCarthy 10-09-2007 20:20

Re: C Programming Homework Help Needed
 
Bump, because I'm still stuck.

Alan Anderson 10-09-2007 20:52

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.

Pat McCarthy 10-09-2007 21:06

Re: C Programming Homework Help Needed
 
Quote:

Originally Posted by Alan Anderson (Post 641595)
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.

Oh yeah, that is completely useless since I implemented the ascii_flag array. Just one for-while type statement is needed.
Still working on it.

Pat McCarthy 10-09-2007 21:53

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
 * Filename:        partiv.c
 * Description:        Takes in a string and prints it out with no letter of the alphabet used more than once.
*/

#include <stdio.h>
#include <string.h>

int main(void);

int main(void)
{
        char temp[19];
        int i, j=0;
        char final[19];
        int primary;
        int length;
        int ascii_flag[128];

        for(i=0;i<128;i++)
        {
                ascii_flag[i]=0;
        }       

        printf("Please enter a word or phrase => ");
        fflush(stdin);
        gets(temp);

        length = strlen(temp);

        /*
        */
        for(primary = 0; primary < length; primary++)
        {
                printf("\nPrimary = %d\n",primary);
                       
                if(ascii_flag[temp[primary]] == 0)
                {                               
                        // Checks for characters (ASCII) that are lowercase or uppercase letters
                        if( ((97 <= (temp[primary])) && ((temp[primary]) >= 122)) == 1)
                        {
                                if(ascii_flag[temp[primary]-32] != 0)
                                {
                                        final[j] = temp[primary];
                                        printf("%c\n",temp[primary]);
                                        j++;
                                        ascii_flag[temp[primary]] = 1;
                                        ascii_flag[temp[primary]-32] = 1;
                                }
                        }
                        else if( (((65 <= (temp[primary])) && ((temp[primary]) >= 90))) == 1)
                        {
                                if(ascii_flag[temp[primary]+32] != 0)
                                {
                                        final[j] = temp[primary];
                                        printf("%c\n",temp[primary]);
                                        j++;
                                        ascii_flag[temp[primary]] = 1;
                                        ascii_flag[temp[primary]+32] = 1;
                                }
                        }
                        // Processes the characters other than letters
                        else// if(ascii_flag[temp[primary]] == 0)
                        {
                                final[j] = temp[primary];
                                j++;
                        }
                }

        }

        printf("\n");
        puts(final);
       
        return 0;
}


Alan Anderson 10-09-2007 22:55

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.

dcbrown 10-09-2007 23:03

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>
#include <ctype.h>

char input[128], *src;
char output[128], *dst;
unsigned long mask;        /* Assumes long is at least 32 bits */
unsigned long flags;

    printf("Please enter a word or phrase => ");
    fflush(stdin);
    gets(input);

    flags = 0;
    dst = &output[0];
    for (src=&input[0]; *src != 0; src++)
    {
        if (isalpha(*src))
        {
              mask = 1<<(tolower(*src)-'a');
              if (flags & mask) continue;
              flags |= mask;
        }
        printf( "%c", *src );
        *dst++ = *src;
    }
    printf( "\n" );
    *dst = 0;
    puts( output );

The original code seems to have its comparisons incorrect?

Code:

if( ((97 <= (temp[primary])) && ((temp[primary]) >= 122)) == 1)
This clause is awkward as written.

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

Pat McCarthy 10-09-2007 23:40

Re: C Programming Homework Help Needed
 
Thanks for the input guys, I've got it working finally.

Code:

/* Author:        Patrick McCarthy
 * Filename:        partiv.c
 * Description:        Takes in a string and prints it out with no letter of the alphabet used more than once.
*/

#include <stdio.h>
#include <string.h>

int main(void);

int main(void)
{
        char temp[19];
        int i, j=0;
        char final[19];
        int primary;
        int length;
        int ascii_flag[26];

        // Initializes the ascii_flag string.
        for(i=0;i<26;i++)
        {
                ascii_flag[i]=0;
        }
       
        // Initializes the final string.
        for(i=0;i<19;i++)
        {
                final[i]='\0';
        }       

        printf("Please enter a word or phrase => ");
        fflush(stdin);
        gets(temp);

        length = strlen(temp);

        for(primary = 0; primary < length; primary++)
        {
//                printf("\nPrimary = %d\n",primary);
                                                       
                        // Checks for characters (ASCII) that are lowercase or uppercase letters
                        if( ('A' <= temp[primary]) && (temp[primary] <= 'Z') )
                        {
                                if(!ascii_flag[temp[primary]-'A'])
                                {
                                        final[j] = temp[primary];
                                        printf("%c\n",temp[primary]);
                                        j++;
                                        ascii_flag[temp[primary]-'A'] = 1;
                                }
                        }
                        else if( ('a' <= temp[primary]) && (temp[primary] <= 'z') )
                        {
                                if(!ascii_flag[temp[primary]-'a'])
                                {
                                        final[j] = temp[primary];
                                        printf("%c\n",temp[primary]);
                                        j++;
                                        ascii_flag[temp[primary]-'a'] = 1;
                                }
                        }
                        // Processes the characters other than letters
                        else
                        {
                                final[j] = temp[primary];
                                j++;
                        }

        }

        printf("\n");
        puts(final);
       
        return 0;
}


Pat McCarthy 14-09-2008 21:35

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)
{
        int num[3];
        int n;
        int flag=0;
        // Range for Base-3 numbers
        int minVal=0, maxVal=2;

        while(flag==0)
        {
                flag=0;
               
                printf("Please enter the Base-3 number one digit at a time,\n");
                printf("starting with the most significant bit.\n");
                printf("MSB ====> "); fflush(stdin); scanf("%d",&num[3]);
                printf("Bit 2 ==> "); fflush(stdin); scanf("%d",&num[2]);
                printf("Bit 3 ==> "); fflush(stdin); scanf("%d",&num[1]);
                printf("LSB ====> "); fflush(stdin); scanf("%d",&num[0]);

                // Diagnostic
                printf("%d%d%d%d",num[3],num[2],num[1],num[0]);

                // Error checking loop
                for(n=3;n>=0;n--)
                {
                        if((num[n] >= minVal) && (num[n] <= maxVal))
                        {
                                if (n==0)
                                        flag=1;
                        }
                        else
                        {
                                printf("\nBit %d = %d",n,num[n]);
//                                printf("\nBit %d is not within the specified base. Try again.\n",n);
                        }
                }
               
                printf("\n");
        }
}



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