Massive Function Problem

This is a discussion on Massive Function Problem within the C Programming forums, part of the General Programming Boards category; Hi All, I am getting really stuck on wriiting some function codes in C. I have got a general understanding ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    15

    Massive Function Problem

    Hi All,

    I am getting really stuck on wriiting some function codes in C. I have got a general understanding of it I just cannot quite get my program to work. I am also missing a couple of functions.

    I will firstly tell you the problem, then I will show the code.

    Write a program that analyzes a single sentence for grammatical statistics. The file should be read from a text file called 1.txt.

    Your program should include 5 functions;
    a)Number of printable characters.
    b)Number of white spaces.
    c)Number of vowels.
    d)Number of consonants.
    e)Frequency of each vowel.

    Once all functions are assembled in your program, use the data to derive and print to screen the following statistics;
    a)String length.
    b)Ratio of printing to non-printing characters.
    c)Ratio of printing to non-printing characters.
    d)Ratio of vowels to consonants.
    e)A list of the frequency with which each vowel appears in the pring as a % of the number of printing characters.

    MY code thus far;

    Code:
    /*assignment 2*/
    #include<stdio.h>
    #include<string.h>
    
    
    int frequency( char[]); 
    int lengthcount(char words[]);
    int vowelfrequency(char words[]);
    
    int main(void)
    {
    	
    	FILE *fp;
    	
    	
    	/**/
    	int frequency_of_each_vowel();
    	/*int i;*/
    	char ch;
    	char sentence[81];
    	int count = 0;						
    	int llengthcount;
    	int pprintablecount;
    	int vowel_count = 0;
    
    	fp = fopen("1.txt","r");								/*Opens up the text file*/
    	
    	
    	if( fp == NULL )										/*If the file does not open a message appears*/
    		printf("File could not be opened\r\n");
    	
    	else
    	{
    		printf("The file contains the following text:\n\n");
    		while ( fscanf(fp,"%c",&ch) != EOF)
    		{
    			sentence[count]=ch;
    			printf("%c",sentence[count]);
    			count++;
    		}
    		sentence[count]='\0';	
    	}
    
    	frequency(sentence);
    
    	fclose(fp); 
    	
    }
    
    int lengthcount(char words[])
    {
    /* set local scope function variables */
    	int llengthcount = 0, i = 0;
    
    	/* while loop to check array for terminating characters */
    	while (words[i] != '\0')
    	{
    		if (words[i] != '\0')
    			llengthcount++;
    		{
    		i++;
    	}
    return llengthcount;
    }
    
    printf("\n\nThe file has a string lenght of: llengthcount %d", llengthcount, words );
    }
    
    /*function to find consonants*/
    //Marc Sharp//
    int frequency( char letters[] )
    {
    	int vowel_count = 0;
    	int count = 0;	
    	int consonant_count = 0 ;
    	int sum = 0, index =0;
    
    	while( letters[index] != '\0')
    	{
    		
    		if( letters[index] == 'b' || letters[index] == 'B' ||
    			letters[index] == 'c' || letters[index] == 'C' ||
    			 letters[index] == 'd' || letters[index] == 'D' ||
    			 letters[index] == 'f' || letters[index] == 'F' ||
    			 letters[index] == 'g' || letters[index] == 'G' ||
    			 letters[index] == 'h' || letters[index] == 'H' ||
    			 letters[index] == 'j' || letters[index] == 'J' ||
    			 letters[index] == 'k' || letters[index] == 'K' ||
    			 letters[index] == 'l' || letters[index] == 'L' ||
    			 letters[index] == 'm' || letters[index] == 'M' ||
    			 letters[index] == 'n' || letters[index] == 'N' ||
    			 letters[index] == 'p' || letters[index] == 'P' ||
    			 letters[index] == 'q' || letters[index] == 'Q' ||
    			 letters[index] == 'r' || letters[index] == 'R' ||
    			 letters[index] == 's' || letters[index] == 'S' ||
    			 letters[index] == 't' || letters[index] == 'T' ||
    			 letters[index] == 'v' || letters[index] == 'V' ||
    			 letters[index] == 'w' || letters[index] == 'W' ||
    			 letters[index] == 'x' || letters[index] == 'X' ||
    			 letters[index] == 'y' || letters[index] == 'Y' ||
    			 letters[index] == 'z' || letters[index] == 'Z' )
    		{
    			consonant_count++;
    		}
    			index++;
    
    	}
    
    	
    	vowel_count = 0;
    	count = 0;
    
    	while ( letters[count] !='\0' ) 				
    	{
    		
    		if (letters[count] == 'a' || letters[count] == 'A' ||
    			letters[count] == 'e' || letters[count] == 'E' ||
    			letters[count] == 'i' || letters[count] == 'I' ||
    			letters[count] == 'o' || letters[count] == 'O' ||
    			letters[count] == 'u' || letters[count] == 'U')
    		{	
    			vowel_count++;					/*Increases the count of vowels each time it passes the loop and finds one*/				
    		}
    		count++;
    		
    	}
    	
    	printf("\n\nThe file has a ratio of: consonants %d : %d vowels.\n\n", consonant_count, vowel_count);
    }
    {
    
    	/*Vowel Frequency*/
    	int vowelfrequency(char words[]);
    	{
    	vowel_count = 0;
    	count = 0;
    
    	while ( letters[count] !='\0' ) 				
    	{
    		
    		if (letters[count] == 'a' || letters[count] == 'A' ||)
    			printf("\n\nThe file has %d Letter A's.\n",vowel_count);
    		else (letters[count] == 'e' || letters[count] == 'E' ||)
    			printf("\n\nThe file has %d Letter E's.\n",vowel_count);
    		else (letters[count] == 'i' || letters[count] == 'I' ||)
    			printf("\n\nThe file has %d Letter I's.\n",vowel_count);
    		else (letters[count] == 'o' || letters[count] == 'O' ||)
    			printf("\n\nThe file has %d Letter O's.\n",vowel_count);
    		else (letters[count] == 'u' || letters[count] == 'U')
    			printf("\n\nThe file has %d Letter U's.\n",vowel_count);
    		{	
    			vowel_count++;					/*Increases the count of vowels each time it passes the loop and finds one*/				
    		}
    		count++;
    
    	return 0;
    }
    	}
    }


    As you can see I have got quite far, I am just missing functions for Number of printable characters and the number of white spaces.

    I am also slightly unsure on how or where to insert the printf statements.

    Any help, no matter how big or how small would be brilliant.

    Thank you in advance.
    Last edited by Marc Sharp; 11-18-2003 at 01:59 PM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,490
    First, edit your post so the code tags actually SURROUND your code, and are not merely just dumped in your message to make the "please use tags" prompt go away
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    15
    Sorry about that, it was a genuine mistake.
    Last edited by Marc Sharp; 11-18-2003 at 01:54 PM.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,490
    Nope, still not right
    Use preview to make sure you get it right
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Visionary Philosopher Sayeh's Avatar
    Join Date
    Aug 2002
    Posts
    212
    Good heavens! All your trying to do is this:

    Your program should include 5 functions;
    a)Number of printable characters.
    b)Number of white spaces.
    c)Number of vowels.
    d)Number of consonants.
    e)Frequency of each vowel.

    Once all functions are assembled in your program, use the data to derive and print to screen the following statistics;
    a)String length.
    b)Ratio of printing to non-printing characters.
    c)Ratio of printing to non-printing characters.
    d)Ratio of vowels to consonants.
    e)A list of the frequency with which each vowel appears in the pring as a % of the number of printing characters.
    Why do you need _so much code_?

    All you need is a little loop and 5 counters. This whole thing can be done (comments and all) under about 25-30 lines of code.
    It is not the spoon that bends, it is you who bends around the spoon.

  6. #6
    Registered User
    Join Date
    Oct 2003
    Posts
    15
    It may sound fairly easy for somebody who writes hundreds of lines of code on a daily basis but for a near beginner it is quite hard, especially as I have been told to use five different functions.

    Any ideas then on how to finish it?

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Here's one example to help you.
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int CountWhiteSpace(const char *s)
    {
      int c;
      
      for (c = 0; *s != '\0'; s++)
      {
        if (isspace(*s)) c++;
      }
      
      return c;
    }
    
    int main(void)
    {
      printf ("Number of white spaces is %d\n", CountWhiteSpace(" blah blah blah"));
      return(0);
    }
    For more information on the is****() functions, read this
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,699
    You can use a single function to count everything. Here's the prototype:

    int count_any(const char s1[], const char s2[]);

    It works like this:

    Set up a loop on 's1' and a nested loop on 's2'. If any two characters match, increment a counter. That way you can do:

    int vowels = count_any(str, "aeiouAEIOU");
    int spaces = count_any(str, " ");

    ...etc.
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

  9. #9
    Registered User
    Join Date
    Oct 2003
    Posts
    15
    So how would I put that as a function? Also where would I put that function in relation to the original code I posted?

    Cheers for the help, it is becoming very handy indeed!

  10. #10
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157
    Originally posted by Sebastiani
    You can use a single function to count everything. Here's the prototype:

    int count_any(const char s1[], const char s2[]);

    It works like this:

    Set up a loop on 's1' and a nested loop on 's2'. If any two characters match, increment a counter. That way you can do:

    int vowels = count_any(str, "aeiouAEIOU");
    int spaces = count_any(str, " ");

    ...etc.
    So you're saying that the requirement stating
    Your program should include 5 functions;
    a)Number of printable characters.
    b)Number of white spaces.
    c)Number of vowels.
    d)Number of consonants.
    e)Frequency of each vowel.

    should be ignored? Software specifications are written for a purpose and must be followed otherwise the boss gets mad.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  11. #11
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,699
    Don't be rediculous. The requirements are in the interface, not the implementation.


    Code:
    int whitespace(const char str[])
    {
     return count_any(str, " ");
    }
    
    int vowels(const char str[])
    {
     return count_any(str, "aeiouAEIOU");
    }

    Reusing code and isolating errors makes deadlines.

    And that makes bosses happy.
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. Problem with function pointers
    By vNvNation in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2004, 06:49 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21