![]() |
| |||||||
![]() |
| | LinkBack | Thread Tools | Display Modes |
| | #1 |
| Registered User Join Date: Oct 2003
Posts: 15
| Setting int pointers to one function (not using five functions!) 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;
}
Cheers for your help Marc |
| Marc Sharp is offline | |
| | #2 |
| +++ OK NO CARRIER Join Date: Oct 2001
Posts: 10,260
| 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 );
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 );
Quzah.
__________________ Hundreds of thousands of dipshits can't be wrong. Are you up for the suck? |
| quzah is offline | |
| | #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. |
| Marc Sharp is offline | |
| | #4 |
| and the hat of vanishing Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,214
| 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. Up to 8Mb PlusNet broadband from only £5.99 a month! |
| Salem is offline | |
| | #5 | |
| Guest Join Date: Aug 2001
Posts: 4,923
| Quote:
Well, did you try out Quzah'a suggestion? | |
| Sebastiani is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| We Got _DEBUG Errors | Tonto | Windows Programming | 5 | 12-22-2006 05:45 PM |
| Working with random like dice | SebastionV3 | C++ Programming | 10 | 05-26-2006 09:16 PM |
| Game Won't Compile | jothesmo | C++ Programming | 2 | 04-01-2006 04:24 PM |
| c++ linking problem for x11 | kron | Linux Programming | 1 | 11-19-2004 10:18 AM |
| Contest Results - May 27, 2002 | ygfperson | A Brief History of Cprogramming.com | 18 | 06-18-2002 01:27 PM |