Question

C PROGRAM, A FOPEN FILE NEED TO BE CREATED Objective To review reading from a file....

C PROGRAM, A FOPEN FILE NEED TO BE CREATED

Objective

  1. To review reading from a file.
  2. To use records (an instance of a struct)
  1. To use Dynamic Memory Allocation
  1. To create and use a dynamically allocated array of structs
  2. To use enumerated types in a useful manner (more info given on Discussion board!)

The Problem

Bad economic times has forced the UCF administration to think outside the box for alternative methods of income. Specifically, we now have a UCF lottery, KnightsBall. Your have been hired by UCF to decide who gets winnings for the KnightsBall Lottery. UCF has a system that automatically creates a file that contains the name of each ticket purchaser along with the combination of 6 distinct numbers picked by that person. Your job is to use this file in determining everyone's winnings. In particular, here is the payout for matching a certain number of values:

Numbers Matched

Winnings

3

$10

4

$100

5

$10000

6

$1000000

Your program will automatically read in an input file with the data for the ticket purchases.

Then your program will ask the user for the winning combination of numbers. (When playing the KnightsBall lottery, you must pick 6 distinct numbers from the set {1,2,3,...,52,53}.) So 53 is the highest possible number.

The user MUST enter these numbers in ascending order. Once these have been entered, your program should print out the names of all the winners, along with how much money they have won. You are guaranteed that each person playing the lottery has bought EXACTLY one ticket and is listed once in the file.

In your implementation please adhere to the following guidelines:

  1. Use a record (struct) to store information about each person/ticket.
  2. Dynamically allocate an array of these structs to store all the ticket information based on the first number in the file, which represents the number of tickets purchased. 3) Use an enumerated type for the four possible winning values.

Implementation:

You MUST use the EXACT struct as follows (notice the typedef):

typedef struct KnightsBallLottoPlayer { char firstName[20];      char lastName[20];      int numbers[6]

} KBLottoPlayer;

Helpful Reminder: As mentioned in class, and on the notes, this struct is simply a “skeleton”. When you type this into your program, the compiler understands that, at some point in the program, you may reference something called KBLottoPlayer. And when you do reference KBLottoPlayer, the compiler will remember that you made this skeleton and will then know what you are talking about. You will then be able to make (dynamically allocate) space for an instance of one KBLottoPlayer, or as required for this program, an array of these structures.

Note: Since this is typedef’ed, you can simply use “KBLottoPlayer” instead of

“struct KnightsBallLottoPlayer” when referencing this structure throughout the program.

Input File Specification (KnightsBall.in)

You will read in input from a file, " KnightsBall.in". The name MUST BE “KnightsBall.in”. Have this AUTOMATED. Do not ask the user to enter “KnightsBall.in”. You should read in this automatically. (This will expedite the grading process.)

* If there is confusion over the *.in file extension, or the *.out file extension (see Output section below), refer to the discussion boards for my explanation.

The input file your program will take in will have the following format:

The first line will contain a single integer n, the total number of tickets bought. Since you are guaranteed that each person playing purchased only one ticket, this integer n also represents the number of lottery players.

The next 2n lines will contain information about each ticket purchased. The information for a single ticket will be on two lines. The first of the two lines will contain the last name of the ticket purchaser, followed by a space, followed by the first name of the ticket purchaser. Both the first and last name are guaranteed to be 19 characters or less. The following line will contain the six integers chosen by that buyer, all in ascending order, separated by spaces.

User Input Specification

  1. The winning lottery numbers will be valid and entered in ascending order.

Output Format

***NOTE***: You should generate your output to a FILE that you will call “KnightsBall.out”.

You will print to the screen requesting the user to input the 6 winning numbers. However, all remaining output is printed to the file.

Your output should contain one winner per line. Each line of output should be of the following format:

First Last matched X numbers and won $Y.

where First is the first name of the winner, Last is the last name of the winner, X is the number of winning numbers the player picked correctly, and Y is the prize money won.

***WARNING***

Your program MUST adhere to this EXACT format (spacing capitalization, use of dollar signs, periods, punctuation, etc). The graders will use very large input files, resulting in very large output files. As such, the graders will use text comparison programs to compare your output to the correct output. If, for example, you have two spaces between in the output when there should be only one space, this will show up as an error even through you may have the program correct. You WILL get points off if this is the case, which is why this is being explained in detail. Minimum deduction will be 10% of the grade, as the graders will be forced to go to text editing of your program in order to give you an accurate grade.

Again, your output MUST ADHERE EXACTLY to the sample output shown below.

Input & Output Samples

Here is a sample input file:

5

Llewellyn Mark

1 15 19 26 33 46 Young Brian

17 19 33 34 46 47

Cazalas Jonathan

1 4 9 16 25 36 Siu Max

17 19 34 46 47 48 Balci Murat 5 10 17 19 34 47

Note that this is NOT a comprehensive test. You should test your program with different data than is shown here based on the specifications given. The user input is given in italics while the program output is in bold. (Note: The following output is based upon the sample input file shown above.)

Enter the winning Lottery numbers:

17 19 33 34 46 47

Output File: (saved to “KnightsBall.out”:

Mark Llewellyn matched 3 numbers and won $10.

Brian Young matched 6 numbers and won $1000000.

Max Siu matched 5 numbers and won $10000.

Murat Balci matched 4 numbers and won $100.

Grading Details

Your program will be graded upon the following criteria:

  1. Adhering to the implementation specifications listed on the first page.
  2. Your algorithmic design.
  3. Correctness.
  4. Use of Dynamic Memory Allocation. If your program does NOT use DMA, you will NOT get credit for the assignment.
  5. The frequency and utility of the comments in the code, as well as the use of white space for easy readability. (We're not kidding here. If your code is poorly commented and spaced and works perfectly, you could earn as low as 80-85% on it.)
  6. Compatibility to CodeBlocks (If your program does not compile in CodeBlocks, you will get a sizable deduction from your grade.)
  7. Your output MUST adhere to the EXACT output format shown above.

Bonus Points Opportunity:

Note: There is a more efficient way to determine the number of matching tickets compared to the straightforward method of doing two for loops. This method will be discussed during the first week of class and is available on the class notes for that week. If you implement the quicker method, you get an extra 10 bonus points.

Restrictions

Name the file you create and turn in KnightsBall.c. Although you may use other compilers, your program must compile and run using CodeBlocks. Your program should include a header comment with the following information: your name, course number, section number, assignment title, and date. You should also include comments throughout your code, when appropriate.

Deliverables

A single source file named KnightsBall.c turned in through WebCourses.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any doubts, please give me comment...

#include<stdio.h>

#include<stdlib.h>

typedef struct KnightsBallLottoPlayer

{

char firstName[20];

char lastName[20];

int numbers[6];

} KBLottoPlayer;

int main(){

FILE *fp;

fp = fopen("KnightsBall.in", "r");

if(fp==NULL){

printf("Unable to open file!\n");

return 0;

}

int i,j, n, k;

fscanf(fp, "%d", &n);

KBLottoPlayer *players = (KBLottoPlayer *)malloc(n*sizeof(KBLottoPlayer));

for(i=0; i<n; i++){

fscanf(fp, "%s %s", players[i].firstName, players[i].lastName);

for(j=0; j<6; j++)

fscanf(fp, "%d", &players[i].numbers[j]);

}

fclose(fp);

int *lotteryNums = (int *)malloc(6*sizeof(int));

printf("Enter the winning Lottery numbers: \n");

for(i=0; i<6; i++)

scanf("%d", &lotteryNums[i]);

fp = fopen("KnightsBall.out", "w");

for(i=0; i<n; i++){

int cnt = 0;

for(j=0; j<6; j++){

for(k=0; k<6; k++){

if(players[i].numbers[j]==lotteryNums[k])

cnt++;

}

}

if(cnt<=2)

continue;

fprintf(fp, "%s %s matched %d numbers and won $", players[i].lastName, players[i].firstName, cnt);

if(cnt==3)

fprintf(fp, "10");

else if(cnt==4)

fprintf(fp, "100");

else if(cnt==5)

fprintf(fp, "10000");

else if(cnt==6)

fprintf(fp, "1000000");

fprintf(fp, ".\n");

}

return 0;

}

Add a comment
Know the answer?
Add Answer to:
C PROGRAM, A FOPEN FILE NEED TO BE CREATED Objective To review reading from a file....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA Lab -Lottery Calculator IMPORTANT: This lab is best implemented using a loop structure. If you...

    JAVA Lab -Lottery Calculator IMPORTANT: This lab is best implemented using a loop structure. If you implement this without a loop, you would need to run your program once for each test item. Create a java program that will test for winning numbers on a lottery game. Each lottery ticket will have 5 integer numbers. Valid numbers must be integer numbers between 1 and 55 Assume that the winning Lottery ticket is 1 9 15 33 40 Your program is...

  • C++ Objective: Write a program to read a pre-specified file containing salespersons' names and daily sales...

    C++ Objective: Write a program to read a pre-specified file containing salespersons' names and daily sales for some number of weeks. It will create an output file with a file name you have gotten from the user that will hold a summary of sales data. Specifications: 1. You should do a functional decomposition to identify the functions that you will use in the program. These should include reading and writing the files, tallying and showing statistics, etc. 2. The program...

  • Using C++ create a lotto program Lottery Design a program that simulates a lottery. Requirements:  The program...

    Using C++ create a lotto program Lottery Design a program that simulates a lottery. Requirements:  The program should have an array of 5 integers named lottery and should generate a random number in the range of 1 through 99 for each element of the array. The user should enter five digits, which should be stored in an integer array named user. The program is to compare the corresponding elements in the two arrays and keep a count of the digits that...

  • Arrays and reading from a file USE C++ to write a program that will read a...

    Arrays and reading from a file USE C++ to write a program that will read a file containing floating point numbers, store the numbers into an array, count how many numbers were in the file, then output the numbers in reverse order. The program should only use one array and that array should not be changed after the numbers have been read. You should assume that the file does not contain more than 100 floating point numbers. The file name...

  • The input file should be called 'data.txt' and should be created according to the highlighted instructions...

    The input file should be called 'data.txt' and should be created according to the highlighted instructions below. Note that even though you know the name of the input file, you should not hard-code this name into your program. Instead, prompt the user for the name of the input file. The input file should contain (in order): the weight (a number greater than zero and less than or equal to 1), the number, n, of lowest numbers to drop, and the...

  • Write a program **(IN C)** that displays all the phone numbers in a file that match the area code...

    Write a program **(IN C)** that displays all the phone numbers in a file that match the area code that the user is searching for. The program prompts the user to enter the phone number and the name of a file. The program writes the matching phone numbers to the output file. For example, Enter the file name: phone_numbers.txt Enter the area code: 813 Output: encoded words are written to file: 813_phone_numbers.txt The program reads the content of the file...

  • Write a PYTHON program that asks the user for the name of the file. The program...

    Write a PYTHON program that asks the user for the name of the file. The program should write the contents of this input file to an output file. In the output file, each line should be preceded with a line number followed by a colon. The output file will have the same name as the input filename, preceded by “ln” (for linenumbers). Be sure to use Try/except to catch all exceptions. For example, when prompted, if the user specifies “sampleprogram.py”...

  • c++ The program will be recieved from the user as input name of an input file...

    c++ The program will be recieved from the user as input name of an input file and and output.file. then read in a list of name, id# and score from input file (inputFile.txt) and initilized the array of struct. find the student with the higher score and output the students info in the output file obtain the sum of all score and output the resurl in output file. prompt the user for a name to search for, when it find...

  • Write this in a C program please. Structures on Disk The Problem Your task is to...

    Write this in a C program please. Structures on Disk The Problem Your task is to write a program that stores and retrieves structures in a file on disk. The file of structures must remain on the disk once your program ends. You should be able to store structures to the file and retrieve from the file after restarting the program. The record that you will be writing to file has the following structure: struct contact {unsigned long phone_number; long...

  • JAVA Write a program that prompts the user to enter a file name, then opens the...

    JAVA Write a program that prompts the user to enter a file name, then opens the file in text mode and reads it. The input files are assumed to be in CSV format. The input files contain a list of integers on each line separated by commas. The program should read each line, sort the numbers and print the comma separated list of integers on the console. Each sorted list of integers from the same line should be printed together...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT