Write a c++ program That fulfills the following criteria. Objectives ? Count the
ID: 3569444 • Letter: W
Question
Write a c++ program That fulfills the following criteria.
Objectives
? Count the number of letters in a file and display list of results.
? Practice using file I/O, loops, command line arguments, arrays/vectors, functions,
structs, and the Bubble Sort algorithm.
Overview
There are various statistics on which letters of the alphabet are used most often, but
with a programming language it's easy to see for ourselves. In this project we'll make a
program that counts the number of letters in any given file and displays the results.
Basic Requirements
The basic requirements for the letter count program are as follows:
? For each file specified on the command line when you run the program:
o Count the number of letters in the file. Both uppercase and lowercase
letters should be counted as the same letter.
o Display a list of how many occurrences of each letter you find, like this:
o #A's: 113
o #B's: 25
o #C's: 22
o ...
o #Z's: 1
Approach
Here are some tips on completing the program:
? You can either hard-code your program to always open "ssb.txt" for reading or
you can loop through the command-line arguments (argc & argv) and open each
one as a file.
? Declare an array of 26 integers to store the count for each letter. Write a loop that
sets each array element to zero because they don't start at zero automatically.
? Start a loop that reads characters (bytes) from the file one at a time. If the
character is A..Z or a..z, add one to your count at the corresponding index 0..25.
Hint: the example programs at the end of Project 8 show how to read in one
character at a time.
? After the end of the file has been reached, print out the count of how many times
each letter occurred. Hint: loop through the numbers 0..25 and print out a
different letter A..Z and its associated count each time.
Have your program display the letter count as a sorted list, e.g.
#E's: 178
#T's: 137
#A's: 113
...
You can do this any way you want, but here are some suggestions.
? Look up the Bubble Sort algorithm on Google. It's a simple, well-known sorting
algorithm. It's also very slow compared to some of the slicker algorithms, but it's
easy to implement and our data set is small enough to where we won't notice.
Write a function that takes your count array and sorts the counts into order using
Bubble Sort.
? This presents another problem. If you have an array of integers counting the
letters, the letter corresponding to each count is implied by the count's array
index. For example, count[0] is the number of A's, count[1] is the number of B's,
and so on. If you sort the counts into order, you lose track of which letter each
count is paired with.
One solution to this problem is to define a new type of data called a "struct"
(structure). Structs are new variable types that are composed of other existing
variable types. In other words, a single variable of a struct type has multiple other
variables inside it. We can define a struct that contains both an integer count and
a corresponding letter. When we swap around structs using Bubble Sort, both the
count and the letter will get swapped around as a pair.
Structs should be defined before any function prototypes. After a struct is defined
you can use its name as a variable type instead of "int" or "double". To access a
sub-variable (or "member") inside a struct, give the struct variable name, a dot,
and then the member name. For example:
#include <...>
...
struct LetterCount
{
char letter;
int count;
};
// function prototypes
...
int main()
{
LetterCount occurrences[26];
occurrences[0].letter = 'A'; // this could be a loop...
occurrences[0].count = 0;
occurrences[1].letter = 'B';
occurrences[1].count = 0;
...
sort( occurrences, 26 );
}
void sort( LetterCount data[], int size )
{
...
if (data[i].count > data[i+1].count)
{
// swap the contents of data[i] with data[i+1]
LetterCount temp = data[i];
data[i] = data[i+1];
data[i+1] = temp;
}
}
Explanation / Answer
#include<stdio.h>
#define SIZE 50
struct student {
char name[30];
int rollno;
int sub[3];
};
void main() {
int i, j, max, count, total, n, a[SIZE], ni;
struct student st[SIZE];
clrscr();
printf("Enter how many students: ");
scanf("%d", &n);
/* for loop to read the names and roll numbers*/
for (i = 0; i < n; i++) {
printf(" Enter name and roll number for student %d : ", i);
scanf("%s", &st[i].name);
scanf("%d", &st[i].rollno);
}
/* for loop to read ith student's jth subject*/
for (i = 0; i < n; i++) {
for (j = 0; j <= 2; j++) {
printf(" Enter marks of student %d for subject %d : ", i, j);
scanf("%d", &st[i].sub[j]);
}
}
/* (i) for loop to calculate total marks obtained by each student*/
for (i = 0; i < n; i++) {
total = 0;
for (j = 0; j < 3; j++) {
total = total + st[i].sub[j];
}
printf(" Total marks obtained by student %s are %dn", st[i].name,total);
a[i] = total;
}
/* (ii) for loop to list out the student's roll numbers who
have secured the highest marks in each subject */
/* roll number who secured the highest marks */
for (j = 0; j < 3; j++) {
max = 0;
for (i = 0; i < n; i++) {
if (max < st[i].sub[j]) {
max = st[i].sub[j];
ni = i;
}
}
printf(" Student %s got maximum marks = %d in Subject : %d",st[ni].name, max, j);
}
max = 0;
for (i = 0; i < n; i++) {
if (max < a[i]) {
max = a[i];
ni = i;
}
}
printf(" %s obtained the total highest marks.", st[ni].name);
getch();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.