• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

Yet another C problem

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

BLcK

Member
Joined
Aug 8, 2005
Location
Ca, Los Angles
What im trying to do is use the input paragraph(at the bottom) through redirection in a command line and put each word into a structure array.What i think is wrong is how my input function passes a word to the record function - logical and syntax wise
also i will have to alphabetize the array of structures.
Code:
#include <stdio.h>
#include <stdlib.h>
typedef struct Word
{
        char word[10];
        int lineNumber;
        int count;
}WORD;

void input(WORD *);
void sort(WORD *);
void output();

int main(void) 
{
    WORD *structArray = (WORD *) malloc( 100*sizeof(WORD) );
    memset( structArray, 0, sizeof(structArray) );
    input(structArray);
    sort(structArray);
    output(structArray);
    system("pause");
    return 0;
}

void input(WORD *structArrayPointer) 
{
     char ch;
     int lineCounter = 1, i = 0, number = 0;
     char *bufferPtr;
     bufferPtr = (char *) malloc( 10 * sizeof(char) );
     bufferPtr[9] = '\0';
     while ( ( ch = getchar() ) != EOF ) 
     {
           if ( ch != ' ' && ch != '.' && ch != ',' || ch != '\n')
           {
                bufferPtr[i] = ch;
           }
           else
           {
               i = 0;           //reset the buffer for next word
               if (ch == '\n') lineCounter++;
               strcpy(structArrayPointer[number].word, bufferPtr);
               structArrayPointer[number].lineNumber = lineCounter;
               number++;                             //keeps the entries in order
           }
           i++;
     }
}
void output(WORD *structArrayPointer)
{
     int i;
     for(i = 0; i < 100; i++)
     {
           printf("word #%d is %s\n", i + 1, structArrayPointer[i].word );
           printf("Its on line number %d\n", structArrayPointer[i].lineNumber );
           printf("It appears %d time(s)\n", structArrayPointer[i].count );
     }                      
}

void sort(WORD *structArrayPtr)
{
     int i, temp;
     WORD tempStruct;
     for (i = 0; i < 100, i++)
     {
         temp = strcmp( structArrayPtr[i].word, structArrayPtr[i+1].word);
         if (temp == 0){} //same
         
         if (temp > 0) //s1>s2
         {
                  tempStruct = structArrayPtr[i];
                  structArrayPtr[i] = structArrayPtr[i+1];
                  structArrayPtr[i+1] = tempStruct;
         }      
     }
}

/*           Input Paragraph
The following are the laws of computer programming. Any given program, when running, is obsolete. Any
given program costs more and takes longer. If a program is useful, it will have to be changed. If a program
is useless, it will have to be documented. Any given program will expand to fill all the available memory.
the value of a program is proportional to the weight of its output. Program complexity grows until it
exceeds the capability of the programmer who must maintain it. Make it possible for programmers to write
programs in English, and you will find that programmers cannot write in English.
*/
 
Last edited:

MRD

Senior Member
Joined
Feb 14, 2003
Can you describe what it's supposed to do and how it is not doing that? What is it doing now?

Please post the output of running it from the command line.
 
OP
BLcK

BLcK

Member
Joined
Aug 8, 2005
Location
Ca, Los Angles
the code should look less confusing after a good friend of mine pointed out quite a few things.

-=updated first post again=-

to do:
-get the code to record to structs properly
-test the sort function
-debug the sort function
-try to optimize the output function
-dynamically allocate memory and expand the lineNumber in WORD(the structure - will change its name later) to hold all the line numbers a word appears on
-list how many times a word appears on one line
 
Last edited:
OP
BLcK

BLcK

Member
Joined
Aug 8, 2005
Location
Ca, Los Angles
BLcK said:
the code should look less confusing after a good friend of mine pointed out quite a few things.

-=updated first post again=-

to do:
-get the code to record to structs properly
-test the sort function
-debug the sort function
-try to optimize the output function
-dynamically allocate memory and expand the lineNumber in WORD(the structure - will change its name later) to hold all the line numbers a word appears on
-list how many times a word appears on one line


This should really clarify things now.

output looks like this:
Code:
word #1 is 
Its on line number 0
It appears 0 time(s)
word #2 is 
Its on line number 0
It appears 0 time(s)
word #3 is 
Its on line number 0
It appears 0 time(s)
word #4 is 
Its on line number 0
It appears 0 time(s)
word #5 is 
Its on line number 0
It appears 0 time(s)
word #6 is 
Its on line number 0
It appears 0 time(s)
word #7 is 
Its on line number 0
It appears 0 time(s)
word #8 is 
Its on line number 0
It appears 0 time(s)
word #9 is 
Its on line number 0
It appears 0 time(s)
word #10 is 
Its on line number 0
It appears 0 time(s)
...................all the way to................
word #75 is 
Its on line number 0
I
im not sure why it stops there - something i have no idea why its stopping there.
Code:
 
OP
BLcK

BLcK

Member
Joined
Aug 8, 2005
Location
Ca, Los Angles
Redid the algorithm which is meant to create structs for word entries as it finds them during runtime

also my compiler is telling me i have this line wrong: "(structArrayPointer + i) = (Word *) malloc( sizeof(struct Word) );" where Word is undclared in the input function. I suspect its in the sizeof() portion of that statement.

Code:
#include <stdio.h>
#include <stdlib.h>
struct Word
{
        char word[10];
        int lineNumber;
        int count;
};

void input(struct Word *structArrayPointer, int counter);
void sort(struct Word *structArrayPointer);
void output(struct Word *structArrayPointer, int pcounter);

int main(void) 
{
    struct Word structArray[1]= { "s1", 1, 2 };
    int counter = 0;
//    memset( structArray, 0, sizeof(structArray) );
    input(structArray, counter);
    sort(structArray);
    output(structArray, counter);
    system("pause");
    return 0;
}

void input(struct Word *structArrayPointer, int pcounter) 
{
     char ch;
     int lineCounter = 1, i = 0, number = 0;
     char buffer[10];
     buffer[10] = '\0';
     while ( ( ch = getchar() ) != EOF ) 
     {
           if ( ch != ' ' && ch != '.' && ch != ',' || ch != '\n')
           {
                buffer[i] = ch;
           }
           else
           {
               i = 0;           //reset the buffer for next word
               if (ch == '\n') lineCounter++;
               strcpy(structArrayPointer[number].word, buffer);
               structArrayPointer[number].lineNumber = lineCounter;
               number++;                             //keeps the entries in order
           }
           (structArrayPointer + i) = (Word *) malloc( sizeof(struct Word) );
           (pcounter)++;    // counts all the words
           i++;
     }
}

void sort(struct Word *structArrayPtr)
{
     int i, temp;
     struct Word tempStruct;
     for (i = 0; i < 100; i++)
     {
         temp = strcmp( structArrayPtr[i].word, structArrayPtr[i+1].word);
         //if (temp == 0){} //same
         
         if (temp > 0) //s1>s2 perform swap
         {
                  tempStruct = structArrayPtr[i];
                  structArrayPtr[i] = structArrayPtr[i+1];
                  structArrayPtr[i+1] = tempStruct;
         }      
     }
}

void output(struct Word *structArrayPointer, int pcounter)
{
     int i;
     for(i = 0; i < pcounter; i++)
     {
           printf("word #%d is %s\n", i+1, structArrayPointer[i].word );
           printf("Its on line number %d\n", structArrayPointer[i].lineNumber );
           printf("It appears %d time(s)\n", structArrayPointer[i].count );
     }                     
}
:bang head :temper: :bang head
 
Last edited: