Thread: Homework help Array Arguments

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    2

    Homework help Array Arguments

    I have an assignment to write a program that calculates how many of each character an imput contains and list the characters according to frequency greater to lesser. I thought I might have figured it out by modifying a piece of code from the textbook decribing array arguments, but when I attempted to run the program it aborted. Would someone please tell me what I need to do differently?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    # define N 52
    
    int find_largest(int a[], int n);
    int find_largest(int a[], int n)
    {
          int i, max;
    
          max = a[1];
          for (i = 1; i < n; i++)
              if (a[i] > max)
              max = a[i];
          return max;
     }
    main()
    {
          int a[N];
          int count = 0;
          int i = 0;
          int largest;
          int *b;
          float percent;
          char ch;
    
        for (i = 0; i < 53; i++)
              a[i] = 0;
    
    
          printf ("Enter a line of text: ");
          ch = getchar();
          while (ch != '\n')
          {
           count++;
           if (ch == ' ')
            a[0]++;
            if (ch >= 'A' && ch <= 'Z')
            {
               i = ch;
                a[i - 64]++;
            }
               if (ch >= 'a' && ch <= 'z')
               a[ch - 70]++;
           ch = getchar();
           }
          printf ("\n\nFREQUENCY TABLE\n---------------\nChar Count %% of Total \n");
          printf ("---- ----- ---------- \nALL    %d    100.00%%\n", count);
    
          largest = find_largest(b, N);
    
          for (i = 0; i < 53; i++)
          {
             if (a[i] != 0)
             {
               if (i == 0)
    
                  ch = ' ';
                 else if (i >= 1 && i <= 26)
                  ch = i + 64;
                  else
                  ch = i + 70;
               percent = (float)(a[i]) / (float)(count) * 100;
               printf ("\"%c\"     %d     %.2f%%\n", ch, b, percent);
              }
          }
    
    
          system("pause");
          return 0;
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
        for (i = 0; i < 53; i++)
              a[i] = 0;
    array overrun

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    2

    Array overrun

    I have some other problem than that... It runs fine when I take out
    Code:
    int find_largest(int a[], int n);
    int find_largest(int a[], int n)
    {
        int i, max;
    
        max = a[1];
        for (i = 1; i < n; i++)
            if (a[i] > max)
            max = a[i];
        return max;
     }
    and
    Code:
          int largest;
          int *b;
          largest = find_largest(b, N);
    then change b to a[i]
    It just doesn't list the output according to frequency.
    Last edited by VanillaSparkles; 08-24-2007 at 01:12 AM.

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    39
    Hi,
    mistake is.

    Code:
    int find_largest(int a[], int n)  \\ You Have Passed (b,N); To it , 'N' is rite But pointer 'b' doesn't have any address to Array. Why??????????
    {
          int i, max;
    
          max = a[1];
          for (i = 1; i < n; i++)     \\ Why You Start The Array From 1 \\
              if (a[i] > max)
              max = a[i];
          return max;
     }
    ok

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-25-2008, 01:50 AM
  2. array of pointers/pointer arithmetic
    By tlpog in forum C Programming
    Replies: 18
    Last Post: 11-09-2008, 07:14 PM
  3. 1-D array
    By jack999 in forum C++ Programming
    Replies: 24
    Last Post: 05-12-2006, 07:01 PM
  4. two dimensional dynamic array?
    By ichijoji in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2003, 04:27 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM