Wednesday, December 12, 2018

Day 21 11/6/18

Modify 1. Modify the program so that it prints the long string and the short string. 2. Modify the program so that it allows the user to enter the long string and the short string. 3. Modify the program so that the long string is read from a data file and the short string is input from the user. 4. Modify the program so that it will work with either lowercase or uppercase characters. 5. Modify the program so that it checks to be sure that the length of the short string is less than the length of the long string.


/* This program initializes a long character string and a short */
/* character string. It then prints the locations of the short */
/* string in the long string. It also prints the number of */
/* occurrences of the short string in the long string. */
#include <stdio.h>
#include <string.h>
#define FILENAME "DNA1.txt"
#define MAX_SIZE 1000
int main(void)
{
/* Declare and initialize variables. */
FILE *dna;
dna = fopen(FILENAME,"r");
if (dna == NULL){
printf("Error opening input file. \n");
}
else{
int count=0;
int size1;
int size2;
int val = 0;
char long_str[MAX_SIZE], short_str[MAX_SIZE];
while(val==0){
printf("how long is the long string?\n");
scanf("%i", &size1);
printf("how long is the short string?\n");
scanf("%i", &size2);
if(size2>size1){
printf("Error short string cannot be larger than long string, please reenter all credentials\n");
}
if(size1>=size2){
val++;
}
}
for(int a = 0;a<size1;a++){
fscanf(dna,"%c",&long_str[a]);
}
printf("enter the short string\n");
for(int b = 0;b<size2;b++){
scanf("%s", &short_str[b]);
if(short_str[b]==97){
short_str[b]=65;
}
if(short_str[b]==99){
short_str[b]=67;
}
if(short_str[b]==116){
short_str[b]=84;
}
if(short_str[b]==103){
short_str[b]=71;
}
}
char *ptr1=long_str, *ptr2=short_str;
/* Count the number of occurrences of short_str in long_str. */
/* While the function strstr does not return NULL, increment */
/* count and move ptr1 to next character of the long string. */
while ((ptr1=strstr(ptr1,ptr2)) != NULL)
{
printf("location %i \n",ptr1-long_str+1);
count++;
ptr1++;
}
/* Print number of occurrences. */
printf("number of occurrences: %i \n",count);
printf("Long string: ");
for(int c = 0;c<size1;c++){
printf("%c", long_str[c]);
}
printf("\nShort string: ");
for(int d = 0;d<size2;d++){
printf("%c", short_str[d]);
}
}
/* Exit program. */
fclose(dna);
return 0;
}

These problems relate to the program developed in this section. 1. Modify the program so that it also counts and prints the number of whorls for the hand. 2. Modify the program so that it also counts and prints the number of arches for the hand. 3. Modify the program so that it also counts and prints the number of loops for the hand. 4. Modify the program so that it combines problems 1, 2, and 3 and thus prints the numbers of the three different categories for the fingertips. 5. Modify the program in problem 4 so that it prints the percentages of the three different categories for the fingertips.


/* This program stores fingerprint information in a structure. */
/* It then references a function to compute the overall category.*/
#include <stdio.h>
#include <math.h>
/* Define a structure for the fingerprint information. */
/* The order for fingertips is right hand, thumb to pinky, */
/* and left hand, thumb to pinky. The codes are L for loops, */
/* W for whorls, and A for arches. */
struct fingerprint
{
int ID_number;
double overall_category;
char fingertip[10];
};
int main(void)
{
/* Declare and initialize variables. */
struct fingerprint new_print;
double compute_category(struct fingerprint f);
void count(struct fingerprint f);
/* Specify information for the new fingerprint. */
new_print.ID_number = 2491009;
new_print.overall_category = 0;
new_print.fingertip[0] = 'W';
new_print.fingertip[1] = 'L';
new_print.fingertip[2] = 'L';
new_print.fingertip[3] = 'W';
new_print.fingertip[4] = 'A';
new_print.fingertip[5] = 'L';
new_print.fingertip[6] = 'L';
new_print.fingertip[7] = 'W';
new_print.fingertip[8] = 'A';
new_print.fingertip[9] = 'L';
/* Reference function to compute overall category. */
new_print.overall_category = compute_category(new_print);
/* Print overall category computed by the function. */
printf("Fingerprint Analysis for ID: %i \n",new_print.ID_number);
printf("Overall Category: %.2f \n",new_print.overall_category);
count(new_print);
/* Exit program. */
return 0;
}
/*--------------------------------------------------------------*/
/* This function computes the overall category */
/* for a fingerprint. */
double compute_category(struct fingerprint f)
{
/* Declare and initialize variables. */
double Rt=0, Ri=0, Rm=0, Rr=0, Rp=0, Lt=0, Li=0, Lm=0, Lr=0,
Lp=0, num, den;
/* Set values based on whorls. */
if (f.fingertip[0] == 'W')
Rt = 16;
if (f.fingertip[1] == 'W')
Ri = 16;
if (f.fingertip[2] == 'W')
Rm = 8;
if (f.fingertip[3] == 'W')
Rr = 8;
if (f.fingertip[4] == 'W')
Rp = 4;
if (f.fingertip[5] == 'W')
Lt = 4;
if (f.fingertip[6] == 'W')
Li = 2;
if (f.fingertip[7] == 'W')
Lm = 2;
/* Compute the numerator and denominator for overall category. */

num = Ri + Rr + Lt + Lm + Lp + 1;
den = Rt + Rm + Rp + Li + Lr + 1;
return num/den;
}
void count(struct fingerprint f){
int countw=0, countl=0, counta=0;
float percentl, percenta, percentw;
if (f.fingertip[0] == 'W'){
countw++;
}
if (f.fingertip[1] == 'W'){
countw++;
}
if (f.fingertip[2] == 'W'){
countw++;
}
if (f.fingertip[3] == 'W'){
countw++;
}
if (f.fingertip[4] == 'W'){
countw++;
}
if (f.fingertip[5] == 'W'){
countw++;
}
if (f.fingertip[6] == 'W'){
countw++;
}
if (f.fingertip[7] == 'W'){
countw++;
}
if (f.fingertip[8] == 'W'){
countw++;
}
if (f.fingertip[9] == 'W'){
countw++;
}
percentw=((countw*100)/10);
printf("the number of whorls is %i \nthe percentage of the fingertips being whorls is %f%%\n", countw, percentw);
if (f.fingertip[0] == 'L'){
countl++;
}
if (f.fingertip[1] == 'L'){
countl++;
}
if (f.fingertip[2] == 'L'){
countl++;
}
if (f.fingertip[3] == 'L'){
countl++;
}
if (f.fingertip[4] == 'L'){
countl++;
}
if (f.fingertip[5] == 'L'){
countl++;
}
if (f.fingertip[6] == 'L'){
countl++;
}
if (f.fingertip[7] == 'L'){
countl++;
}
if (f.fingertip[8] == 'L'){
countl++;
}
if (f.fingertip[9] == 'L'){
countl++;
}
percentl=((countl*100)/10);
printf("the number of loops is %i \nthe percentage of the fingertips being loops is %f%%\n", countl, percentl);
if (f.fingertip[0] == 'A'){
counta++;
}
if (f.fingertip[1] == 'A'){
counta++;
}
if (f.fingertip[2] == 'A'){
counta++;
}
if (f.fingertip[3] == 'A'){
counta++;
}
if (f.fingertip[4] == 'A'){
counta++;
}
if (f.fingertip[5] == 'A'){
counta++;
}
if (f.fingertip[6] == 'A'){
counta++;
}
if (f.fingertip[7] == 'A'){
counta++;
}
if (f.fingertip[8] == 'A'){
counta++;
}
if (f.fingertip[9] == 'A'){
counta++;
}
percenta=((counta*100)/10);
printf("the number of arches is %i \nthe percentage of the fingertips being arches is %f%%\n", counta, percenta);
return;

}

No comments:

Post a Comment