i need your help Banfa!!!!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • euniceno1
    New Member
    • Oct 2006
    • 11

    i need your help Banfa!!!!

    the problem is to count the letters and sequence from a file. the fist thing of my code is to read file and in the main function i create is to count the characters and sequence by using 2 dimension array.
    where the character include letters and spaces.
    I am just learning c language at the moment, i have problem with following program:
    #define TRUE 1
    #define FALSE 0
    #define MAX_LENGTH 100000 /* Max number of chars read from file */
    #include <stdio.h>
    #include <ctype.h>

    int Get_String_From _File(char filename[], char input[])
    /* Reads in characters (including \n) from a file as one long string */
    /* filename should give name of an existing file;
    input will return contents of that file */
    /* Return value 0 if file open fails, 1 otherwise */
    /* Maximum length of string is stored in MAX_LENGTH */

    {
    int i = -1; /* will be updated before being used */
    char temp;
    FILE *infile;

    infile = fopen(filename, "r");
    if (infile == NULL) {
    printf("Failed to open file \"%s\"\n",filen ame);
    return FALSE;
    }

    do {
    i++;
    input[i] = fgetc(infile);
    } while (input[i] != EOF && i < MAX_LENGTH - 1 && input[i] != '\0');
    if (i == MAX_LENGTH - 1) /* only reason for stopping is MAX_LENGTH */
    fprintf(stderr, "Warning: Reached maximum size of file handled by this program!\n\n");
    input[i] = '\0'; /* end of string marker*/
    fclose(infile);

    return TRUE; /* Successfully read file */
    }
    int main(void)
    {
    int c, i, j, spaces,charArra y[26][26];

    for (i=0; i<26; ++i) /* init array to zero */
    charArray[i][j] = 0;
    for( j= 0; j<26; ++j)
    charArray[j] = 0;
    while((c = getchar()) != EOF) /* count the letter */
    if(isupper(c) && islower(c))
    ++char[c -> 'A' && c -> 'a' && c->’ ‘];
    for (i=0; i<26; ++i)
    if(i%6==0)
    for ( j= 0; j<26; ++j)
    if(j%6==0) {
    printf("\n");
    printf("%4c%4c: %3d%3d", 'A' || 'a' +i +j, letter[i][j]);
    }
    printf("\n\n");
    return 0;
    }
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by euniceno1
    the problem is to count the letters and sequence from a file. the fist thing of my code is to read file and in the main function i create is to count the characters and sequence by using 2 dimension array.
    where the character include letters and spaces.
    I am just learning c language at the moment, i have problem with following program:
    #define TRUE 1
    #define FALSE 0
    #define MAX_LENGTH 100000 /* Max number of chars read from file */
    #include <stdio.h>
    #include <ctype.h>

    int Get_String_From _File(char filename[], char input[])
    /* Reads in characters (including \n) from a file as one long string */
    /* filename should give name of an existing file;
    input will return contents of that file */
    /* Return value 0 if file open fails, 1 otherwise */
    /* Maximum length of string is stored in MAX_LENGTH */

    {
    int i = -1; /* will be updated before being used */
    char temp;
    FILE *infile;

    infile = fopen(filename, "r");
    if (infile == NULL) {
    printf("Failed to open file \"%s\"\n",filen ame);
    return FALSE;
    }

    do {
    i++;
    input[i] = fgetc(infile);
    } while (input[i] != EOF && i < MAX_LENGTH - 1 && input[i] != '\0');
    if (i == MAX_LENGTH - 1) /* only reason for stopping is MAX_LENGTH */
    fprintf(stderr, "Warning: Reached maximum size of file handled by this program!\n\n");
    input[i] = '\0'; /* end of string marker*/
    fclose(infile);

    return TRUE; /* Successfully read file */
    }
    int main(void)
    {
    int c, i, j, spaces,charArra y[26][26];

    for (i=0; i<26; ++i) /* init array to zero */
    charArray[i][j] = 0;
    for( j= 0; j<26; ++j)
    charArray[j] = 0;
    while((c = getchar()) != EOF) /* count the letter */
    if(isupper(c) && islower(c))
    ++char[c -> 'A' && c -> 'a' && c->’ ‘];
    for (i=0; i<26; ++i)
    if(i%6==0)
    for ( j= 0; j<26; ++j)
    if(j%6==0) {
    printf("\n");
    printf("%4c%4c: %3d%3d", 'A' || 'a' +i +j, letter[i][j]);
    }
    printf("\n\n");
    return 0;
    }
    What if Banfa has gone away for fishing trip in the Victoria falls and decides to spend a month there and there are half a dozen people who can help you who are around but seeing as they would that they are not Banfa...

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      Originally posted by r035198x
      What if Banfa has gone away for fishing trip in the Victoria falls and decides to spend a month there and there are half a dozen people who can help you who are around but seeing as they would that they are not Banfa...
      Very true, I suspect r035198x probably could have answered you problem.

      Anyway you have given the specification of the program you are writing, you have given the code, you do not seem to have stated what the problem you are having is.

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by Banfa
        Very true, I suspect r035198x probably could have answered you problem.

        Anyway you have given the specification of the program you are writing, you have given the code, you do not seem to have stated what the problem you are having is.
        Hopefully you won't be taking a holiday away from the internet of course Banfa!
        I'm just wondering what the OP is trying to do with a statement like
        Code:
        if(isupper(c) && islower(c))

        Comment

        • euniceno1
          New Member
          • Oct 2006
          • 11

          #5
          I mean the capital and small letter doesnt count as seperately!

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            if(isupper(c) && islower(c))

            Read it as English, it says

            "If c is an uppercase letter and c is a lowercase letter then do something"

            Pretend c is the letter g, will this or wont this do something


            Also I am intrigued to know if this line of code actually compiles and what you are trying to achieve with it

            ++char[c -> 'A' && c -> 'a' && c->’ ‘];

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by Banfa
              if(isupper(c) && islower(c))

              Read it as English, it says

              "If c is an uppercase letter and c is a lowercase letter then do something"

              Pretend c is the letter g, will this or wont this do something


              Also I am intrigued to know if this line of code actually compiles and what you are trying to achieve with it

              ++char[c -> 'A' && c -> 'a' && c->’ ‘];
              Yes. Intrigued is certainly the word for it.

              Comment

              • euniceno1
                New Member
                • Oct 2006
                • 11

                #8
                The code:
                ++char[c -> 'A' && c -> 'a' && c->’ ‘];
                I want the code do the following things:
                character can be counted as they are capital letter and small letter and spaces.
                But the gcc compiler said this is parse errors before 'A', and 'a', and' ' . I don't know how to correct it!

                Comment

                • Banfa
                  Recognized Expert Expert
                  • Feb 2006
                  • 9067

                  #9
                  I assume you meant to use charArray instead of char then in

                  ++char[c -> 'A' && c -> 'a' && c->’ ‘];

                  However

                  Code:
                  for (i=0; i<26; ++i) /* init array to zero */
                      charArray[i][j] = 0;
                  for( j= 0; j<26; ++j) 
                      charArray[j] = 0;
                  does not initialise charArray to 0. The first attempt to set charArray[i][j] happens before j has been set to anything, in the second attempt charArray[j] is no an l-value so I doubt this compiles.

                  Think about what you need to do to access each member of charArray in turn.

                  Also it is not clear why you have chosen to use a 2D array


                  back to

                  ++char[c -> 'A' && c -> 'a' && c->’ ‘];

                  So I am going to assume that you meant charArray and that charArray is in fact singly dimensioned.

                  You have a character of interest c and you want to increment one value in the array charArray to reflect this.

                  you are going to need a line something like

                  charArray[ix]++;

                  to increment the array value, which leaves calculating ix given c, valid ranges of c (in ASCII) are

                  'a' <= c <= 'z'
                  'A' <= c <= 'Z'
                  c == ' '

                  That is 53 different characters in total so ix will have to have the range 0-52.

                  So your task is to produce a method to calculate ix in the range 0 - 52 given c in the valid ranges I just gave.

                  Comment

                  • euniceno1
                    New Member
                    • Oct 2006
                    • 11

                    #10
                    But if I only use single array I can't count the sequence? And the question need 2 D array!

                    Comment

                    • euniceno1
                      New Member
                      • Oct 2006
                      • 11

                      #11
                      Please some one help me! the first thing of the problem is to count letters, regardless about the capital or small and spaces, cormas, numbers as one catagories that means the array side are 27 second things is to count sequences, the 2 letters that are together for example aa, ab, ac etc that means I need a 2 D array, But I don't know how to correct my code. So some one please help me!!!!

                      Comment

                      • Banfa
                        Recognized Expert Expert
                        • Feb 2006
                        • 9067

                        #12
                        Actually then you need a 1D array to count ocurances and a 2D array to count sequences.

                        Start with just counting the letters, we can get onto sequences. My last post had some rather strong hints on how to do this.

                        If you are still having trouble then post your current code (assuming it has changed) with the compile errors and indicate why you are unable to correct the errors (if there are a lot of errors only list the first few).

                        Comment

                        • euniceno1
                          New Member
                          • Oct 2006
                          • 11

                          #13
                          The first fist half of the code are reading the File, that is correct, so I just post my main function which is incorrect somehow:
                          int main(void)
                          {
                          int count, c, i, spaces, char[27];
                          for (i = 0; i < 27; ++i) {
                          count = 0;
                          while((c=getcha r()) != EOF) /* count the letter */
                          if(isupper(c) && islower(c))

                          charArray[i]++;
                          while('a'<=c<=' z'
                          'A' <= c <= 'Z'c == ' ')
                          for(i=0;i<26;++ i)
                          if(i%6==0)
                          {
                          printf("\n");
                          printf("%4c:%3d ", 'A' || 'a' +i , char[i]);
                          }
                          printf("\n\n");
                          return 0;
                          }
                          And I don't know how can I correct my 2d array:
                          int main ()
                          int count,c, i, j, charArray[26][26];
                          for (i = 0; i < 26; ++i) {
                          count = 0;
                          for( j= 0; j<26; ++j)
                          count = 0;
                          while((c=getcha r()) != EOF) /* count the letter */
                          if(isupper(c) && islower(c))
                          for(i=0;i<26;++ i)
                          if(i%6==0)
                          for(j=0;j<26;++ j)
                          if(j%6==0){
                          printf("\n");
                          printf("%4c%4c: %3d%3d", 'A' || 'a' +i +j, char[i][j]);
                          }
                          printf("\n\n");
                          return 0;
                          }

                          Comment

                          • euniceno1
                            New Member
                            • Oct 2006
                            • 11

                            #14
                            Can you Help me!!!
                            The first fist half of the code are reading the File, that is correct, so I just post my main function which is incorrect somehow:
                            int main(void)
                            {
                            int count, c, i, spaces, char[27];
                            for (i = 0; i < 27; ++i) {
                            count = 0;
                            while((c=getcha r()) != EOF) /* count the letter */
                            if(isupper(c) && islower(c))

                            charArray[i]++;
                            while('a'<=c<=' z'
                            'A' <= c <= 'Z'c == ' ')
                            for(i=0;i<26;++ i)
                            if(i%6==0)
                            {
                            printf("\n");
                            printf("%4c:%3d ", 'A' || 'a' +i , char[i]);
                            }
                            printf("\n\n");
                            return 0;
                            }
                            And I don't know how can I correct my 2d array:
                            int main ()
                            int count,c, i, j, charArray[26][26];
                            for (i = 0; i < 26; ++i) {
                            count = 0;
                            for( j= 0; j<26; ++j)
                            count = 0;
                            while((c=getcha r()) != EOF) /* count the letter */
                            if(isupper(c) && islower(c))
                            for(i=0;i<26;++ i)
                            if(i%6==0)
                            for(j=0;j<26;++ j)
                            if(j%6==0){
                            printf("\n");
                            printf("%4c%4c: %3d%3d", 'A' || 'a' +i +j, char[i][j]);
                            }
                            printf("\n\n");
                            return 0;
                            }
                            Thank you for any suggestion

                            Comment

                            • Banfa
                              Recognized Expert Expert
                              • Feb 2006
                              • 9067

                              #15
                              I suggest that you fix all the compil errors this code produces.

                              As I have already said if you are getting compiler errors that you do not understand or are unable to fix then post the error indicating on which line of code they are happening, however here are a few tips

                              None of these are correct C syntax

                              int count, c, i, spaces, char[27];

                              while('a'<=c<=' z'
                              'A' <= c <= 'Z'c == ' ')

                              printf("%4c:%3d ", 'A' || 'a' +i , char[i]);


                              Trying to redefine main in the middle of main is just wrong.

                              Comment

                              Working...