I need a program C or C for my homework about joint and cond

I need a program (C or C++) for my homework about joint and conditional probability. It will work with .txt files, the program should read \".txt\" file and count how many lower case in this \".txt\" file and it should calculate their probabilities (joint and conditional probability).

data text l MD Plaa (22) p (3/2

Solution

#include<stdio.h>
#include<stdlib.h>

int main()
{
   char ch;
   char fileName[100];
   int chCount[26] = {0};
   int countTot = 0;
   FILE *fp;
   int i = 0;
   int option = 0;
   float probJ,probC;
   char chCheck[26];

   printf(\"Enter file name: \");
   scanf(\"%s\",fileName);   // Read file name

   fp = fopen(fileName,\"r\");
   if (fp == NULL){
      printf(\"Error while opening file.\ \");
      return 1;
   }
   while((ch = fgetc(fp)) != EOF){
      countTot++;               // Store total character count
      if (ch >= \'a\' && ch <= \'z\')
      {
   chCount[ch - \'a\']++;   // Store count for each character
      }
   }

   do {
      printf(\"Options: \ 1. Joint probability \ 2. Condtional probability \ \");
      printf(\" 0. Exit \ Enter your option: \");
      scanf(\"%d\",&option);   // Read option
      probJ = 1.0;
      probC = 0.0;
      if (option == 1 || option == 2){
   printf(\"Enter characters as string: \");
   scanf(\"%s\", chCheck); // Read characters to calculate probability
   for (i = 0; i<26; i++)
   {
        ch = chCheck[i];
        if (ch == \'\\0\')
           break;
        if (ch < \'a\' || ch > \'z\')
           continue;
        if (option == 1){
           probJ = probJ * ((float)chCount[ch - \'a\']/countTot); // Calculate joint probability
        } else {
           probC = probC + ((float)chCount[ch - \'a\']/countTot); // Calculate conditional probability
        }
   }
   if (option == 1)
        printf(\"Joint probability = %f\", probJ);   //display
   else
        printf(\"Conditional probability = %f\", probC); //display
      }
   } while (option != 0); //Check exit condition
   fclose(fp);
   return 0;
}

I need a program (C or C++) for my homework about joint and conditional probability. It will work with .txt files, the program should read \
I need a program (C or C++) for my homework about joint and conditional probability. It will work with .txt files, the program should read \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site