Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

reading file objects

Name: Anonymous 2011-07-12 13:02

I have some quick questions that maybe you could all help me out with.

So, I'm coding his program that needs to read a text file, and write a text file that includes a list of all the unique words that are present in the input text file and displays some word statistics, which include: shortest word, longest word, word length average, and standard deviation.
The program will read a maximum of 50 valid lines from the input file. A valid line is one that contains at least one word. Each line contains a maximum of 50 words, and the maximum length of each word is 20 characters.

My trouble is reading the text into an array without duplicates (or even at all), I'm not sure how to go about doing that, or whether or not it should be multidimensional. It was suggested to maybe do an array such as array[50][50][21] but I was not positive why they chose 3-dimensions.

Any help is greatly appreciated, I can show you examples of inputs and outputs if it would help.

Name: !!IROarA5jkrb1FsX 2011-07-12 13:31

>>1
What language do you want me to help you with?

Name: Anonymous 2011-07-12 13:32

>>1
no

Name: !!L6zziHG8/KZx9ph 2011-07-12 13:34

jew

Name: !!IROarA5jkrb1FsX 2011-07-12 13:35

Ignore >>3,4 for they are elitist faggots.

Name: Anonymous 2011-07-12 13:35

>>2
Here is what I have so far, and I will show you an example of the input and output:

http://pastebin.com/3WMSswbr

It goes-

code
======
input example
======
output example

Name: !!1ILcdKBYf7nyXyy 2011-07-12 13:43

printf("Please input the filename of the text file in this directory: ");
scanf("%s", list);

There is a semantic error in printf(). Also you don't have scanf() check for a return value.

printf("Wrong file, please try again: ");
scanf("%s", list);

Ditto.

while (fscanf(ifp, "%s", word)!=EOF

This loop will also test when '== EOF'. I don't think this is what you want. Besides, in standard C, it is possible for the function to have a successful return and still have an error. For example, if you have a bad disk sector on a drive.

Name: Anonymous 2011-07-12 13:50

Ignore this douchefag >>7, and go with C++ instead.
some minor hints:


std::string word;
std::cin >> word;
// ...
while(getline(std::cin, line))  /* ... */

Name: Anonymous 2011-07-12 13:55

>>8
Yo homegirl, I'm just following the ANSI/ISO specifications. I know this might seem kind of a radical concept for you to grasp since you work as some monkey IT person at a hick firm.

Name: Anonymous 2011-07-12 13:57

>>8
So now, you want the OP to ditch C in favor of a language that is bloated with objects? Nice job. You're below average.

Name: Anonymous 2011-07-12 14:12

>>10
>bloated with objects
IHBT

Name: Anonymous 2011-07-12 14:14

>>11
How do you figure that you moron? C is smaller than C++ because it doesn't have all the OOP crap.

Name: Anonymous 2011-07-12 14:15

>>10
AS FAST AS C VROOM VROOM -funroll-loops -Ofast -fomit-frame-pointer VROOOOM!!!, right? Fuck you.

Name: Anonymous 2011-07-12 14:22

>>13
Don't be all burt hurt because you are using a language that is inferior to stuff like Lisp and Smalltalk.

Name: >>2 !!IROarA5jkrb1FsX 2011-07-12 14:23

Hope this helps

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


typedef struct Word {
    struct Word *next;
    int occurrences;
    char p[0];
} Word;



void add_word(Word *L, char *str) {
    while (L->next) {
        L = L->next;
        if (!strcmp(L->p, str)) { // check if the strings are equal
            L->occurrences++;
            return;
        }
    }
    L->next = malloc(sizeof(Word) + strlen(str));
    L = L->next;
    L->next = NULL;
    L->occurrences = 1;
    strcpy(L->p, str);
}

char *find_max(Word *L) {
    int max;
    char *maxp = NULL;
    while (L->next) {
        L = L->next;
        if (L->occurrences > max) {
            maxp = L->p;
            max = L->occurrences;
        }
    }
    return maxp;
}

int main() {
   
    Word head;
    head.next = NULL;
   
    add_word(&head, "dick");
    add_word(&head, "cock");
    add_word(&head, "penis");
    add_word(&head, "dick");
    add_word(&head, "dick");
    add_word(&head, "penis");
    add_word(&head, "dick");
    add_word(&head, "penis");
   
    printf("the most common form of male genitalia is the %s\n", find_max(&head));
   
    /* I'm too drunk to do string manipulation in C. sorry */
   
}

Name: Anonymous 2011-07-12 14:25

>>14
Both Lisp and Smalltalk are incredibly unaesthetic.

Name: >>15 !!IROarA5jkrb1FsX 2011-07-12 14:32

>>1
Also, if you have any questions to the above code, which is a simple implementation of a linked list, don't be afraid to ask.  You might also note that in the worst case (where no words are similar), the run time is O(n2) http://en.wikipedia.org/wiki/Big_O_notation , because every time you add a word you end up traversing the entire list then adding it to the end.  It's not that bad if the working set is small and the words repeat a lot.  But if you want to go past the scope of your assignment (which you should really finish before experimenting any further), you could try and implement a hash tablehttp://en.wikipedia.org/wiki/Hash_table .  Well, good luck!

Name: >>17 !!IROarA5jkrb1FsX 2011-07-12 14:33

s/to the above code/relative to the above code/;

Name: Anonymous 2011-07-12 15:17

>>16
I bet you think Michael Bay movies are aesthetic.

Name: Anonymous 2011-07-12 15:19

>>19
Never seen a single one of his movies.

Name: Anonymous 2011-07-12 16:03

>>20
you are uncultured swine

Name: Anonymous 2011-07-12 16:20

>>15
std::vector would make your highlevel assembly readable and less memory-leaky. you catch my drift here buddy?

Name: Anonymous 2011-07-12 16:37

>>22
std::AIDS would make your highlevel assembly readable and less anally-leaky. you catch my drift here slab?

Name: Anonymous 2011-07-12 16:50

>>23
why is thee enraged?

Name: Anonymous 2011-07-12 16:53

>>24
I'm not even mad.  But if I were to use the Sepples STL, I'd use std::hash_map, if you catch my multitrack drift.

Name: Anonymous 2011-07-12 17:58

>>25
I couldn't catch your drift because our drift ABI is not compatible :(

Name: Anonymous 2011-07-12 21:26

>>6
this doesn't look like python code...

Name: Anonymous 2011-07-12 22:15

>>27
too bad phyton is for fucking faggots

Name: Anonymous 2011-07-12 22:44


scanf("%s", list);


buffer overflow.

Name: Anonymous 2011-07-13 3:43

>>27
Who said it was?

Name: Anonymous 2011-07-13 3:57

>>28
learn to spell, lipthfagstorm

Name: Anonymous 2011-07-13 4:12

>>12
C++ is bloated when you compare it with C, just like a Japanese man is bloated when you compare him with the average African.

Name: Anonymous 2011-07-13 4:20

>>31
learn to capitalize, phython cock sucker

Name: Anonymous 2011-07-13 4:49

>>33
fuck off and die, lithp scum

Name: Anonymous 2011-07-13 5:14

>>34
ur language is gay and so u are eat shit faggot

Name: Anonymous 2011-07-13 6:32

>>35
my language is not python faggot also eat shit and die motherufcking fag

Don't change these.
Name: Email:
Entire Thread Thread List