Need help in C

Discussion in 'Software' started by MadWolf, May 9, 2005.

  1. MadWolf

    MadWolf Private E-2

    Hi I think somebody would help me. I am a rookie in programming so i can't tackle one problem which I think will be easy for you. I have to write a program in C. The task is: To read one file. Then to create stack which elements should be rows. Then to put into stack words that contain only letters and have odd number of symbols. The result should be putted in separate file. And the last thing is to count average number of symbols in the word.
    Please help me otherwise my lecturer will crush me
     
  2. Maxwell

    Maxwell Folgers

    Umm, didn't the lecturer give you any notes?! and isn't crushing illegal?

    Anyway rather than do your homework for you. The things you need to use include:

    main, exit
    fopen, fscanf, fclose, fprintf (or system call equivalent) for all file I/O.
    strlen
    printf
    { } ; = + / and maybe %

    There is no need for a "atack" as the processing can be done on the fly and roughly the psedo-code is:

    Code:
    set sum of word lengths to zero.
    set number of words to zero.
    
    open file1.
    open file2.
    while reading from the file1...
      read a line from the file1.
      for each word in the line...
        accumulate count of number of words.
        get the length of the word
        accumulate sum of word lengths.
        if the word length is odd then
          for each character in the word...
            if the character is non-alphbetic then
              skip this word and process the next word on the line.
            endif
          endfor
          print the word to file2.
        endif
      endfor
    endwhile
    close file2.
    close file1.
    
    divide sum of word lengths by total number of words.
    print the average result.
     
  3. MadWolf

    MadWolf Private E-2

    Thanks for your help. Well but the main problem is that i don't know how to use everything with stack. Because lecturer didn't say anything about arrows, pointers etc. If it isn't a problem for you please give me more tips how i should write my program.
     
  4. Maxwell

    Maxwell Folgers

    I did say that you don't need to use "stacks" to perform the operation that you need to do. That is to filter out the words in one file and put them to another file. However, you may need to consider a "stack" to hold the line you've just read and to parse the line.

    --------
    In brief and note that this is brief and only touches the surface of pointers and arrays: A stack or an array is simply a means of storing data from other sources into internal memory. There are two types, static or dynamic.

    Static arrays are fixed at compile time of a program and the allocated memory cannot be changed whilst the program is running. Static data is declared as is at the head of your code block. For example an array of 100 integers is declared thus:

    Code:
    .
    .
    .
    static const integer_array_size = 100;
    .
    .
    .
    {
        int integer_array[integer_array_size] ;
    .
    .
    .
    }
    The size of the static array can be determined by the number of initial values that assign to it. It is preferable to set the "magic" numbers for static array sizes as constants in your program either via macros or as a static const.


    Dynamic arrays determine the memory allocation whilst the program is running and are accessed through pointers. Dynamic allocation is defined using the memory allocation routines malloc, calloc and realloc and the memory is released using a call to free - see http://seth.positivism.org/man.cgi/3/malloc. Example allocating 100 integer array elements, de-allocate it and then allocating 1000 integer array elements:

    Code:
    {
        int *integer_pointer;
    .
    .
    .
        integer_pointer = (int *) calloc ( (size_t) 100, sizeof ( int ) ) ;
        if ( integer_pointer ==  NULL )
        {
            /* Handle the error when memory cannot be allocated. */
    .
    .
    .
        }
    .
    .
    .
        free ( (void * ) integer_pointer ) ;
        integer_pointer = (int *) NULL ;
    .
    .
    .
        integer_pointer = (int *) calloc ( (size_t) 1000, sizeof ( int ) ) ;
    .
    .
    .
        free ( (void * ) integer_pointer ) ;
        integer_pointer = (int *) NULL ;
    .
    .
    .
    )
    Here the size of the array allocation is determined whilst the program is running and typically the values of 100 and 1000 are read from some external source, i.e., not hard-coded in the program, as in this example. Note the essential type casting. calloc is preferential to using malloc as it initialises your "array" with zeroes. However, you can use memset to initialise your allocated memory. Always free your memory once you have no further need of it and set it to point to nowhere, i.e., address zero. This is so that you do not rely on the values referenced from the pointer and prevent memory leaks - DO NOT rely on automatic variables to free your memory once the variable goes out of scope or rely on the value of an automatic variable being retained unless you explicitly declare it as a static pointer.

    Access to the elements within both types of "array" is the same for the simple one-dimensional arrays that are being considered here. Your arrays count from zero. For example the following all reference the data value held at the first position in your array:

    integer_array [ 0 ]
    *integer_pointer
    integer_pointer [ 0 ]

    To access the last element of your array the following are equivalent:

    integer_array [ 99 ]
    *(integer_pointer + 99)
    integer_pointer [ 99 ]

    Note the pointer de-referencing by using *.

    Things get a bit more interesting when you start to use arrays of characters - called strings. If you use "stacks" in your homework you will need to do this and use arrays of strings.

    However, the equivalence between pointers and arrays fail when you start to use two- or more dimensional arrays since the remaining non-primary dimensions are not fixed for dynamic arrays and problems occur when trying to pass what is thought to be inherent array sizes to other routines or procedures.

    Here is a tutorial on pointers and arrays: http://pweb.netcom.com/~tjensen/ptr/pointers.htm
     
  5. MadWolf

    MadWolf Private E-2

    Thanks a lot for your help. I hope I'll write my program without any other problems.
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds