[ create a new paste ] login | about

Link: http://codepad.org/1ldEz3fp    [ raw code | output | fork ]

C, pasted on Dec 10:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>

void printRules();
int decisionToPlay(char choice);
void playKeno(int gameArray[], int userArray[], int matchArray[], FILE *output);
int choseNums(int userArray[]);
void sortNums(int userArray[], int userIndex);
void randNums(int gameArray[]);
int compareNums(int gameArray[], int userArray[], int* matchArray[], int userIndex);
void printNums(int gameArray[], int matchArray[], int userIndex);
void writeNums(int gameArray[], int userArray[], int matchArray[],int userIndex, FILE *output);

int main (void) {
   
	char choice; // decision to play game
	int gameArray[80] = {0}; // array of pseudo-randomly chosen numbers
	int userArray[15]; // array of read elements
	int matchArray[15]; // array of matched values
	FILE *output; // results of game are written to this file
	
	output = fopen( "Prog6Output.txt", "w");
	if(output != NULL){
		
	   srand(time(NULL));
	   printRules();
    
	   while (decisionToPlay(choice)) {
		   playKeno(gameArray,userArray,matchArray,output);
	   } // end while
	} // end if
	else {
		printf("Prog6Output.txt not found! Ending Program. . .");
	} // end else
     
    return 0;
} // end main
/*  ====================== printRules ===========================
 This functions prints the rules of the game.
 Pre  none 
 Post none
*/
void printRules() {

	printf("        Keno Game Rules\n");
	printf("The computer will draw 20 numbers\n");
    printf("between 1 and 80.  To play the game\n");
	printf("choose 1 to 15 numbers. Your goal is to\n");
	printf("match a higher percentage of your choices\n\n");
} // end printRules
/*  ====================== decisionToPlay =======================
 This functions reads true or false into choice (decision to play 
 game)
 Pre  none 
 Post return true or false
*/
int decisionToPlay(char choice) {

	printf("Do you want to play? (n for No): ");
	scanf("%c", &choice);
	choice = toupper(choice);
	
	if (choice == 'N') {
		return 0;
	}
	else {
		return 1;
	}	
} // end decisionToPlay
/*  ====================== playKeno ==============================
  This function calls subfunctions choseNums, sortNums, randNums, 
  compareNums, printNums, and writenums
  Pre  none 
  Post assigns choseNums to userIndex, matches to compareNums, and 
       writes output to file
*/
void playKeno(int gameArray[], int userArray[], int matchArray[], FILE *output) {
    
	int userIndex; // the index of userArray
	int matches; // the amount of numbers that are the same in gameArray and userArray
	
	userIndex = choseNums(userArray);
	sortNums(userArray,userIndex);
	randNums(gameArray);
	matches = compareNums(gameArray,userArray,&matchArray,userIndex);
	printNums(gameArray,matchArray,userIndex);
	writeNums(gameArray,userArray,matchArray,userIndex,output);
	
} // end playKeno
/*  ====================== choseNums =============================
 This function reads tempIndex value of numbers and assigns to 
 userArray
 Pre  none 
 Post returns tempIndex, assigns values to userArray 
*/
int choseNums(int userArray[]) {

	int tempIndex; // the temp index of userArray
	
	printf("How many numbers to you want to choose? (1 to 15): ");
	scanf("%d", &tempIndex);
	
	for (int i = 0; i < tempIndex; i++) {
		printf("Enter choice 1 (1 to 80): ");
		scanf("%d", &userArray [i]);
	}
	return tempIndex;
} // end choseNums
/*  ====================== sortNums ===============================
 This function
 Pre  
 Post 
*/
void sortNums(int userArray[], int userIndex)
{
	int i1, i2;
	int smallIndex;
	int temp; // same type as each element
	
	for( i1 = 0; i1 < userIndex-1 ; ++i1 ) {
		smallIndex = i1;
		for( i2 = i1 + 1; i2 < userIndex; ++i2){
			if( userArray[i2] < userArray[smallIndex] ){
				smallIndex = i2;
			} // end if
		} // end for i2
		temp = userArray[i1];
		userArray[i1]= userArray[smallIndex];
		userArray[smallIndex] = temp;
	} // end for i1
} // end sortNums
/*  ====================== randNums ===============================
 
 Pre  
 Post 
*/
void randNums(int gameArray[]) {

	for (int i = 0; i < 20; i++) {
		gameArray[i] = ((rand() % (80-1+1) ) + 1);
	}
	
    // need to write some code to get rid of repitions in the generated numbers

} // end randNums
/*  ====================== compareNums ===============================
 
 Pre  
 Post 
 */
int compareNums(int gameArray[], int userArray[], int* matchArray[], int userIndex) {

	int i;
	
	for (i = 0; i < userIndex ; i++) {
		if (gameArray[i] == userArray[i]) {
			gameArray[i] == *matchArray[i];
		}
	}
	
	return i-1;
}
/*  ====================== printNums =================================
 
 Pre  
 Post 
 */
void printNums(int gameArray[],int matchArray[], int userIndex) {
    
	printf("Keno Game Numbers: ");
	
	for( int i = 0; i < 20; ++i ){
		printf("%5d ", gameArray[i]);
	} // end for
	printf("\n");
	printf("Your matches out of %d: ", userIndex);
	for( int i = 0; i < 20; ++i ){
		printf("%5d ",matchArray[i]);
	} // end for
}
/*  ====================== writeNums =================================
 
 Pre  
 Post 
 */
void writeNums(int gameArray[], int userArray[], int matchArray[],int userIndex, FILE *output) {

	fprintf(output, "Game Numbers: ");
	for( int i = 0; i < 20; ++i ){
		fprintf(output, "%5d ", gameArray[i]);
	} // end for
    fprintf(output,"\n");
	fprintf(output, "Numbers you chose: ");
	for (int i = 1; i < userIndex; ++i) {
		fprintf(output, "%5d", userArray[i]);
	} // end for
    fprintf(output,"\n");
	fprintf(output, "Matched numbers (out of %d): ", userIndex);
	for (int i = 1; i < userIndex; ++i) {
		fprintf(output, "%5d", matchArray[i]);
	} // end for
}


Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
In function 'choseNums':
Line 105: error: 'for' loop initial declaration used outside C99 mode
In function 'randNums':
Line 141: error: 'for' loop initial declaration used outside C99 mode
In function 'printNums':
Line 174: error: 'for' loop initial declaration used outside C99 mode
Line 179: error: redefinition of 'i'
Line 174: error: previous definition of 'i' was here
Line 179: error: 'for' loop initial declaration used outside C99 mode
In function 'writeNums':
Line 191: error: 'for' loop initial declaration used outside C99 mode
Line 196: error: redefinition of 'i'
Line 191: error: previous definition of 'i' was here
Line 196: error: 'for' loop initial declaration used outside C99 mode
Line 201: error: redefinition of 'i'
Line 196: error: previous definition of 'i' was here
Line 201: error: 'for' loop initial declaration used outside C99 mode


Create a new paste based on this one


Comments: