Question

In: Computer Science

Adding code that will keep cumulative count of characters? Hello, I am having issues in figuring...

Adding code that will keep cumulative count of characters?

Hello, I am having issues in figuring out how to keep letter from a .txt file with the declaration of independence, this is my code, I can keep track of the frequency but can't figure out the way to keep adding the total characters, as the you go down the alphabet the number is supposed to be counting up

#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include

#define DEBUG

void main()
{
   FILE* fp;

   int numChars[26] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }; //26 characters
   unsigned char c;
   int ndx;
   if ((fp = fopen("DOI.txt", "r")) != NULL)
   {
       while (1)
       {
           c = fgetc(fp);

           if (feof(fp))
           {
               break;
           }

           if (!isalpha(c))
           {
               continue;
           }

           ndx = toupper(c) - 'A';
           numChars[ndx]++;
       }

       printf("Letter\tFrequency\tTotal Characters\n");
       printf("------\t---------\t---------------\n");
       for (int i = 0; i < 26; i++)
       {
           printf("%c\t%3d\t%d\n", i + 'A', numChars[i]); //Here is where I should add the counter but can't figure out how to make it work
       }

       fclose(fp);
   }
   else
   {
       perror("Unable to open file");
   }

   getchar();
}

Solutions

Expert Solution

//Updated code is in the bold text

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
void main()
{
FILE* fp;
int numChars[26] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }; //26 characters
unsigned char c;
int ndx;
if ((fp = fopen("DOI.txt", "r")) != NULL)
{
while (1)
{
c = fgetc(fp);//get the character

if (feof(fp))
{
break;
}

if (!isalpha(c))
{
continue;
}

ndx = toupper(c) - 'A';
numChars[ndx]++;
}

printf("Letter\tFrequency\tTotal Characters\n");
printf("------\t---------\t---------------\n");
int s=0;
for (int i = 0; i < 26; i++)
{
s=s+numChars[i];//take one extra variable add that to previous value and print that to get cumulative value
printf("%c\t%3d\t\t%d\n",i + 'A',numChars[i],s); //Here is where I should add the counter but can't figure out how to make it work
}

fclose(fp);
}
else
{
perror("Unable to open file");
}
}


Related Solutions

Can you balance these redox equations? I am having trouble figuring out the process of adding...
Can you balance these redox equations? I am having trouble figuring out the process of adding extra products to make them work. ClO3- + Cl- -----> Cl2 + ClO2- Cr2O72- + C2O42- -----> Cr3+ + CO2
I am having difficulties with figuring out the formulas for this this question. Is there a...
I am having difficulties with figuring out the formulas for this this question. Is there a walk-through for this? You are the manager of a monopoly that sells a product to two groups of consumers in different parts of the country. Group 1’s elasticity of demand is -2, while group 2’s is -3. Your marginal cost of producing the product is $30. a. You are a monopolist. b. You compete against one other firm in a Cournot oligopoly. c. You...
I am having an issue with the code. The issue I am having removing the part...
I am having an issue with the code. The issue I am having removing the part when it asks the tuter if he would like to do teach more. I want the program to stop when the tuter reaches 40 hours. I believe the issue I am having is coming from the driver. Source Code: Person .java File: public abstract class Person { private String name; /** * Constructor * @param name */ public Person(String name) { super(); this.name =...
I'm having trouble figuring out the constraints to this problem. I know that I am maximizing...
I'm having trouble figuring out the constraints to this problem. I know that I am maximizing 55x + 45y, however the variables are throwing me off. I only need help on question #1 as it would be a great help to understanding the rest of what the questions are asking. The problem is as follows: NorCal Outfitters manufactures a variety of specialty gear for outdoor enthusiasts. NorCal has decided to begin production on two new models of crampons: the Denali...
I have figured out this assignment, but I am having a hard time figuring out the...
I have figured out this assignment, but I am having a hard time figuring out the extra credit, Thanks! Creating a Rectangle class We have created a representation of a rectangle in C++ and extended it to create multiple representation's of rectangles. Classes can simplify that process considerably. In this assignment, your task is to convert the representation of a rectangle we created earlier in the semester to a Rectangle class. The Rectangle class shall consist of the following: The...
Python Coding Question (Data Science) Hello I am having difficulty writing a code in python to...
Python Coding Question (Data Science) Hello I am having difficulty writing a code in python to do a specific task. I have two text files, Positive.txt and Practice_forhw1.txt. I want to write a script that will check if any words in Practice_forhw1.txt match any of the words in Positive.txt then the word would get replaced with a "+1" in the Practice_forhw1.txt and then print out the Practice_forhw1.txt.  (i.e if the word "happy" is in both Positive.txt and Practice_forhw1.txt then I want...
These are the correct answers but I am having difficulty figuring out how these figures were...
These are the correct answers but I am having difficulty figuring out how these figures were reached. On March ​1, 2018​, Nailtique Nail Salons issued $450,000 of 15​-year, 5 percent bonds payable. The bonds were sold for $432,000. The bonds pay interest each August 31 and February 28​, and any discount or premium is amortized using​ straight-line amortization. Requirement 1. Fill in the blanks to complete each statement. a. Nailtique Nail Salons' bonds are priced at (express the price as...
I am having difficulty figuring out the rest of this problem, primarily with regards to calculating...
I am having difficulty figuring out the rest of this problem, primarily with regards to calculating APIC and Retained earnings for Stock Dividends. Also, I need assistance with the fourth problem, how to calculate the total value of shareholders' equity. Could you please help show me how to solve? Accounting for Share Transactions The shareholders' equity section of the consolidated balance sheet of Wilson Industries appeared as follows at the beginning of the year: Shareholders' Equity Class A common stock,...
Please fix this code I am having issues compiling it all together there is 3 codes...
Please fix this code I am having issues compiling it all together there is 3 codes here and it's giving me errors in my main method..... I feel like it might be something simple but I can't seem to find it. package assignement2; import java.util.ArrayList; import java.util.Scanner; public class reg1 { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter the number of items: "); int number = input.nextInt(); input.nextLine(); for (int i = 0; i <...
I am needing assistance figuring out how to write the code for this project in C#...
I am needing assistance figuring out how to write the code for this project in C# (C sharp)... Create a new Project and name it - InClassParticipation3 Get up to 5 inputs from the user. The user must enter at least 1 number. After the first input, ask them if they would like to input another number if they say 'yes' get the provided input and multiply it to the other inputs provided by the user. If the user says...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT