pointers code no lb
#include <stdio.h>
#include <string.h>
#define MAX_SIZE 1000
#define FILENAME "DNAString1.txt"
int main(void)
{
/* Declare and initialize variables. */
int count=0;
char long_str[MAX_SIZE];
char short_str[MAX_SIZE];
FILE *DNA;
DNA = fopen(FILENAME, "r");
if (DNA == NULL)
printf("Error opening input file. \n");
else
{
fscanf(DNA,"%s",long_str);
printf("Enter in short DNA string: \n");
scanf("%s",short_str);
char *ptr1=long_str, *ptr2=short_str;
int short_length = strlen(short_str);
int long_length = strlen(long_str);
if (short_length > long_length)
printf("Error: Short string must be smaller than long string.\n");
else
{
printf("\nLong DNA String: %s \n", long_str);
printf("Short DNA String: %s \n", short_str);
while ((ptr1=strstr(ptr1,ptr2)) != NULL)
{
//printf("location of the string \n", ptr1, long_str);
printf("location %i \n",ptr1-long_str+1);
count++;
ptr1++;
}
/* Print number of occurrences. */
printf("number of occurrences: %i \n",count);
fclose(DNA);
}
}
/* Exit program. */
return 0;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Tsunami Data:
#include <stdio.h>
#define FILENAME "waves2.txt"
/* Define structure to represent a tsunami. */
struct tsunami
{
int mo, da, yr, fatalities;
double max_height;
char location[20];
};
int main(void)
{
/* Declare variables. */
int k=0, npts;
double max=0, sum=0, ave;
struct tsunami t[100];
FILE *waves;
/* Read and print information from the file. */
waves = fopen(FILENAME,"r");
if (waves == NULL)
printf("Error opening data file. \n");
else
{
while (fscanf(waves,"%d %d %d %lf %d %s",&t[k].mo,&t[k].da,
&t[k].yr,&t[k].max_height,&t[k].fatalities,t[k].location[20]) == 6)
{
sum = sum + t[k].max_height;
if (t[k].max_height > max)
max = t[k].max_height;
k++;
}
npts = k;
ave = sum/npts;
printf("Summary Information for Tsunamis \n");
printf("Maximum Wave Height (in feet): %.2f \n",max*3.28);
printf("Average Wave Height (in feet): %.2f \n",ave*3.28);
printf("Tsunamis with greater than average heights: \n");
for (k=0; k<=npts-1; k++)
if (t[k].max_height > ave)
printf("%s \n",t[k].location);
fclose(waves);
}
return 0;
}
/* It then references a function to compute the overall category.*/
#include <stdio.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 arches=0, loops=0, whorls=0;
int main(void)
{
/* Declare and initialize variables. */
struct fingerprint new_print;
double compute_category(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);
printf("Arches Count: %i, %i%% \n", arches,10*arches);
printf("Loops Count: %i, %i%% \n", loops,10*loops);
printf("Whorls Count: %i, %i%% \n", whorls,10*whorls);
printf("% \n");
/* 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;
for(int i = 0; i <= 9; i++)
{
if (f.fingertip[i] == 'A')
arches++;
if (f.fingertip[i] == 'L')
loops++;
if (f.fingertip[i] == 'W')
whorls++;
}
/* 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;
}
#include <string.h>
#define MAX_SIZE 1000
#define FILENAME "DNAString1.txt"
int main(void)
{
/* Declare and initialize variables. */
int count=0;
char long_str[MAX_SIZE];
char short_str[MAX_SIZE];
FILE *DNA;
DNA = fopen(FILENAME, "r");
if (DNA == NULL)
printf("Error opening input file. \n");
else
{
fscanf(DNA,"%s",long_str);
printf("Enter in short DNA string: \n");
scanf("%s",short_str);
char *ptr1=long_str, *ptr2=short_str;
int short_length = strlen(short_str);
int long_length = strlen(long_str);
if (short_length > long_length)
printf("Error: Short string must be smaller than long string.\n");
else
{
printf("\nLong DNA String: %s \n", long_str);
printf("Short DNA String: %s \n", short_str);
while ((ptr1=strstr(ptr1,ptr2)) != NULL)
{
//printf("location of the string \n", ptr1, long_str);
printf("location %i \n",ptr1-long_str+1);
count++;
ptr1++;
}
/* Print number of occurrences. */
printf("number of occurrences: %i \n",count);
fclose(DNA);
}
}
/* Exit program. */
return 0;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Tsunami Data:
#include <stdio.h>
#define FILENAME "waves2.txt"
/* Define structure to represent a tsunami. */
struct tsunami
{
int mo, da, yr, fatalities;
double max_height;
char location[20];
};
int main(void)
{
/* Declare variables. */
int k=0, npts;
double max=0, sum=0, ave;
struct tsunami t[100];
FILE *waves;
/* Read and print information from the file. */
waves = fopen(FILENAME,"r");
if (waves == NULL)
printf("Error opening data file. \n");
else
{
while (fscanf(waves,"%d %d %d %lf %d %s",&t[k].mo,&t[k].da,
&t[k].yr,&t[k].max_height,&t[k].fatalities,t[k].location[20]) == 6)
{
sum = sum + t[k].max_height;
if (t[k].max_height > max)
max = t[k].max_height;
k++;
}
npts = k;
ave = sum/npts;
printf("Summary Information for Tsunamis \n");
printf("Maximum Wave Height (in feet): %.2f \n",max*3.28);
printf("Average Wave Height (in feet): %.2f \n",ave*3.28);
printf("Tsunamis with greater than average heights: \n");
for (k=0; k<=npts-1; k++)
if (t[k].max_height > ave)
printf("%s \n",t[k].location);
fclose(waves);
}
return 0;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/* - */
/* This program stores fingerprint information in a structure. *//* It then references a function to compute the overall category.*/
#include <stdio.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 arches=0, loops=0, whorls=0;
int main(void)
{
/* Declare and initialize variables. */
struct fingerprint new_print;
double compute_category(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);
printf("Arches Count: %i, %i%% \n", arches,10*arches);
printf("Loops Count: %i, %i%% \n", loops,10*loops);
printf("Whorls Count: %i, %i%% \n", whorls,10*whorls);
printf("% \n");
/* 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;
for(int i = 0; i <= 9; i++)
{
if (f.fingertip[i] == 'A')
arches++;
if (f.fingertip[i] == 'L')
loops++;
if (f.fingertip[i] == 'W')
whorls++;
}
/* 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;
}
Comments
Post a Comment