Thread: Setting int pointers to one function (not using five functions!)

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

    Setting int pointers to one function (not using five functions!)

    I am trying to make the follwing five functions into one function. It counts each vowel and lists the frquency of each vowel.

    The printf statements are fine it is merely the function that I am having problems with. I know that I need to set int pointers to each vowel but I am not entirely sure how to do this.

    Here is my program, please not that I am only interested in the bottom part which lists the frquency's of each vowel (named functions vowelfrequencya e i o u.

    Code:
    #include<stdio.h>
    #include<string.h>
    
    /*Declaring the functions of the program*/
    int printablecount(char[]);			/*The printable characters count funtion*/
    int CountNonPrint (char[]);			/*The non-printable characters count function*/
    int consonant_frequency( char[]);	/*The consonant count function*/
    int vowel_frequency(char[]);		/*The vowel count function*/
    
    
    int vowelfrequencya(char[]);
    int vowelfrequencye(char[]);
    int vowelfrequencyi(char[]);
    int vowelfrequencyo(char[]);
    int vowelfrequencyu(char[]);
    
    int main(void)						/*Declaring Main*/
    {
    	
    
    /*Opens the text file from a remote source*/
    
    	FILE *fp;
    	
    /*Setting the variables*/	
    	char ch;
    	char sentence[81];
    	int count = 0;						
    	int cf, vf, pc, np, vfa, vfe, vfi, vfo, vfu;
    	
    /*Opens the text file from a remote source*/
    	fp = fopen("1.txt","r");								
    	
    /*Sets a null if the file cannot be read from the remote source*/	
    	if( fp == NULL )										
    		printf("File could not be opened\r\n");
    	
    	else
    	{
    	
    /*Prints to screen the following, this includes the text that has been read from the file*/
    		printf("**************************************************************\n\n");
    		printf("Marc Christopher Sharp\n");
    		printf("SID: 9912276\n");
    		printf("APU\n");
    		printf("MSc Computer Science Conversion 2003-4 Semester 1\n");
    		printf("\n");
    		printf("Design and Implementation of Software Systems\n");
    		printf("George Wilson\n");
    		printf("Assignment 2 (30%)\n");
    		printf("A program that analyzes a single sentence for grammatical statistics\n\n");		
    		printf("\n**************************************************************\n\n");
    		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';	
    	}
    
    /*Returns the int values to main from each function*/
    	cf = consonant_frequency(sentence);
    	vf = vowel_frequency(sentence);
    	pc = printablecount(sentence);
    	np = CountNonPrint(sentence);
    	vfa = vowelfrequencya(sentence);
    	vfe = vowelfrequencye(sentence);
    	vfi = vowelfrequencyi(sentence);
    	vfo = vowelfrequencyo(sentence);
    	vfu = vowelfrequencyu(sentence);
    	
    /*Prints the grammatical statistics as required in the assignment*/
    	printf("\n\n**************************************************************\n\n");
    	printf("\n\n**************************************************************\n\n");
    	printf("Grammatical Statistics\n");
    	printf("\n\n**************************************************************\n\n");
    	printf("..............................................................\n");
    	printf(".                                                            .\n");
    	printf(".The string lenth for the sentence is: %d                    .\n",pc+np);
    	printf(".                                                            .\n");
    	printf("..............................................................\n\n");
    	printf("..............................................................\n");
    	printf(".                                                            .\n");
    	printf(".The ratio of printing to non-printing characters is: %d : %d.\n",pc,np);
    	printf(".                                                            .\n");
    	printf("..............................................................\n\n");
    	printf("..............................................................\n");
    	printf(".                                                            .\n");
    	printf(".The ratio of vowels to consonants is: %d : %d               .\n",vf,cf);
    	printf(".                                                            .\n");
    	printf("..............................................................\n\n");
    	printf("..............................................................\n");
    	printf(". Vowel . Frequency . Per Cent of Total Printable Characters .\n");
    	printf("..............................................................\n");
    	printf(".   A   .    %.2d   .                                          .\n",vfa);
    	printf(".   E   .    %.2d   .                                          .\n",vfe);
    	printf(".   I   .    %.2d   .                                          .\n",vfi);
    	printf(".   O   .    %.2d   .                                          .\n",vfo);
    	printf(".   U   .    %.2d   .                                          .\n",vfu);
    	printf("..............................................................\n\n");
    
    
    	
    /*Closes the file as it has been read to main and stored*/
    	fclose(fp); 
    
    /*Indicates to main that the program has ended successfully*/
    	return 0;
    }
    
    
    /*****************************
     * printablecount FUNCTION A *
     * WRITTEN BY: Alex Ferris   *
     *****************************/
    
    /* pass array to function - return int value */
    int printablecount(char words[])
    {
    	/* set local scope function variables*/
    	int pprintablecount = 0,  i = 0;
    
    	/* while loop to check each character in the array until it reaches the terminating character */
    	while (words[i] != '\0')
    	{
    		/* if statements that check characters and increases printable tally if condition is met */
    		if (words[i] != ' ' || words[i] != ' ')
    		
    			pprintablecount++;
    
    	/* increase value of loop counter */
    	i++;
    	}
    
    /* return int value to pc variable in main */
    return pprintablecount;
    }
    
    
    /*********************************
     * Non-printablecount FUNCTION B *
     * WRITTEN BY: Richard Pilling   *
     *********************************/
    
    /* pass array to function - return int value */
    int CountNonPrint (char letters[])					/* count whitespace/non-printing chars */
    {
    	/* set local scope function variables*/
    	int sum=0, index=0;
    
    	/* while loop to check each character in the array until it reaches the terminating character */
    	while(letters[index] !='\0')
    	{	
    		/* if statements that check non-characters and increases non-printable tally if condition is met */
    		if (isgraph(letters[index])==0)
    			sum++;
    		/* increase value of loop counter */
    		index++;
    
    	}
    	/* return int value to np variable in main */
    	return sum;
    
    }
    
    
    /*********************************
     * Consonant Count FUNCTION C    *
     * WRITTEN BY: Marc Sharp        *
     *********************************/
    
    /* pass array to function - return int value */
    int consonant_frequency( char letters[] )
    {
    	/* set local scope function variables*/
    	int consonant_count = 0;
    	int sum = 0, index = 0;
    
    	/* while loop to check each character in the array until it reaches the terminating character */
    	while( letters[index] != '\0')
    	{
    		/* if statements that check consonants and increases consonants tally if condition is met */
    		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++;
    		}
    		/* increase value of loop counter */
    			index++;
    
    	}
    /* return int value to cf variable in main */
    	return consonant_count;
    }
    
    /*********************************
     * Vowel Count FUNCTION D        *
     * WRITTEN BY: Marc Sharp        *
     *********************************/
    
    /* pass array to function - return int value */	
    int vowel_frequency(char letters[])
    {
    	/* set local scope function variables*/
    	int vowel_count = 0;
    	int ccount = 0;
    
    	/* while loop to check each character in the array until it reaches the terminating character */
    	while ( letters[ccount] !='\0' ) 				
    	{
    		/* if statements that check vowels and increases vowels tally if condition is met */
    		if (letters[ccount] == 'a' || letters[ccount] == 'A' ||
    			letters[ccount] == 'e' || letters[ccount] == 'E' ||
    			letters[ccount] == 'i' || letters[ccount] == 'I' ||
    			letters[ccount] == 'o' || letters[ccount] == 'O' ||
    			letters[ccount] == 'u' || letters[ccount] == 'U')
    		{	
    			vowel_count++;					/*Increases the count of vowels each time it passes the loop and finds one*/
    		}
    		/* increase value of loop counter */
    		ccount++;
    		
    	}
    	/* return int value to vf variable in main */
    	return vowel_count;	
    }
    
    
    /*********************************
     * Vowel frequency FUNCTION E    *
     * WRITTEN BY: Marc Sharp        *
     *********************************/
    
    //
    //aaaaaaaaaaaaaaaaaa
    
    int vowelfrequencya(char letters[])
    {
    	int vowel_counta = 0;
    	int acount = 0;
    
    	while ( letters[acount] !='\0' ) 				
    	{
    		
    		if (letters[acount] == 'a' || letters[acount] == 'A')
    			
    		{	
    			vowel_counta++;					/*Increases the count of vowels each time it passes the loop and finds one*/
    		}
    		acount++;
    		
    	}
    return vowel_counta;	
    }
    
    
    ////eeeeeeeeeeeeeeee
    
    int vowelfrequencye(char letters[])
    {
    	int vowel_counte = 0;
    	int ecount = 0;
    
    	while ( letters[ecount] !='\0' ) 				
    	{
    		
    		if (letters[ecount] == 'e' || letters[ecount] == 'E')
    			
    		{	
    			vowel_counte++;					/*Increases the count of vowels each time it passes the loop and finds one*/
    		}
    		ecount++;
    		
    	}
    return vowel_counte;	
    }
    
    
    
    ///iiiiiiiiiii
    
    int vowelfrequencyi(char letters[])
    {
    	int vowel_counti = 0;
    	int icount = 0;
    
    	while ( letters[icount] !='\0' ) 				
    	{
    		
    		if (letters[icount] == 'i' || letters[icount] == 'I')
    			
    		{	
    			vowel_counti++;					/*Increases the count of vowels each time it passes the loop and finds one*/
    		}
    		icount++;
    		
    	}
    return vowel_counti;	
    }
    
    //////////ooooooooooo
    int vowelfrequencyo(char letters[])
    {
    	int vowel_counto = 0;
    	int ocount = 0;
    
    	while ( letters[ocount] !='\0' ) 				
    	{
    		
    		if (letters[ocount] == 'o' || letters[ocount] == 'O')
    			
    		{	
    			vowel_counto++;					/*Increases the count of vowels each time it passes the loop and finds one*/
    		}
    		ocount++;
    		
    	}
    return vowel_counto;	
    }
    
    ///uuuuuuuuuuuuuuuuuuu
    int vowelfrequencyu(char letters[])
    {
    	int vowel_countu = 0;
    	int ucount = 0;
    
    	while ( letters[ucount] !='\0' ) 				
    	{
    		
    		if (letters[ucount] == 'u' || letters[ucount] == 'U')
    			
    		{	
    			vowel_countu++;					/*Increases the count of vowels each time it passes the loop and finds one*/
    		}
    		ucount++;
    		
    	}
    return vowel_countu;	
    }
    I know large parts of the program are completed the complicated way (e.g. the consonant count) but I am fairly happy with it.

    Cheers for your help

    Marc

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    void countstuff( char tobecounted[], int tally[5] )
    {
        ...count a...
        tally[0] = a_count;
    
        ...count e...
        tally[1] = e_count;
    
        ...and so on...
    }
    
    ...elsewhere...
    
    int aeiou[5] = {0};
    countstuff( "count this string please", aeiou );
    Or alternately:
    Code:
    void countstuff( char tobecounted[], int *a, int *e, int *i, int *o, int *u )
    {
        ...count a...
        *a = a_count;
    
        ...count e...
        *e = e_count;
    
        ...and so on...
    }
    
    ...elsewhere...
    int a, e, i, o, u;
    
    countstuff( "count this string please", &a, &e, &i, &o, &u );
    I'll leave the rest up to you. It should be very simple since you've already got all of your functions completed. Just merge them into one of the two.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    15
    I am still not quite sure, I have had a go but cannot get it to work.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well post your attempt at least, then we can help you move forward some more.
    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.

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Originally posted by Marc Sharp
    I am still not quite sure, I have had a go but cannot get it to work.

    Well, did you try out Quzah'a suggestion?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  2. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 09:16 PM
  3. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 04:24 PM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM