/* When a ball is thrown with a speed U m/s at an angle of T radians
with the horizontal, the horizontal distance travelled or range,
R metres, is given by:
R = U * U * sin( 2 * T ) / 9.8
This program produces a table showing the range of the ball
for speeds in the range 10 to 40 m/s in steps of 10 m/s and angles
with the horizontal in the range 15 to 75 degrees in steps of 15
degrees */
#include<stdio.h>
#include<math.h>
main() {
float U,T,S;
float R;
float PI = 3.14159265;
printf("*************************************************\n");
printf("* Speed(m/s) * Angle(degrees) * Range(m) *\n");
printf("*************************************************\n");
for (U = 10.0; U < 41.0; U=U+10.0) {
for (T=15.0; T<76.0; T=T+15.0) {
R = sin((T * 2 * PI / 360) * 2);
R = R/9.8;
R = U * U * R;
printf("* %.0f * %.0f * %6.2f *\n",U, T, R);
}
}
printf("*************************************************\n");
}
with the horizontal, the horizontal distance travelled or range,
R metres, is given by:
R = U * U * sin( 2 * T ) / 9.8
This program produces a table showing the range of the ball
for speeds in the range 10 to 40 m/s in steps of 10 m/s and angles
with the horizontal in the range 15 to 75 degrees in steps of 15
degrees */
#include<stdio.h>
#include<math.h>
main() {
float U,T,S;
float R;
float PI = 3.14159265;
printf("*************************************************\n");
printf("* Speed(m/s) * Angle(degrees) * Range(m) *\n");
printf("*************************************************\n");
for (U = 10.0; U < 41.0; U=U+10.0) {
for (T=15.0; T<76.0; T=T+15.0) {
R = sin((T * 2 * PI / 360) * 2);
R = R/9.8;
R = U * U * R;
printf("* %.0f * %.0f * %6.2f *\n",U, T, R);
}
}
printf("*************************************************\n");
}
/* Reads co-ordinates from a text file and displays them,
calculates area under the curve using either Simpsons rule or the
Trapezium rule.
The contents of the text file could be:
5
0.00 1.00
0.50 0.75
1.00 2.00
1.50 4.75
2.00 9.00
The program is called with 2 arguments, the text file
and then S or T to specify a rule */
#include <stdio.h>
#include <varargs.h>
#include <math.h>
int main(int argc, char*argv[])
{
int i;
int n = 0;
float x[5];
float y[5];
float area = 0;
short count;
FILE *fptr;
char rule;
if (argc < 3)
{
printf("%s: Error, incorrect arguments provided\n",argv[0]);
return 1;
}
if ((fptr = fopen(argv[1], "r")) == NULL)
{
printf("Error opening file\n");
return 3;
}
rule = *argv[2];
fscanf(fptr, "%d", &n);
for (count = 0; count < 5; count++)
{
fscanf(fptr, "%f %f", &x[count], &y[count]);
}
for (count = 0; count < 5; count++)
{
if (rule == 'S') {
for (i = 0; i <= n - 3; i = i + 2)
{
area = area + (x[i + 1] - x[i]) * (y[i] + (4 * y[i + 1]) + y[i + 2]) / 3;
}
}
else if (rule == 'T') {
for (i = 0; i <= n - 2; i++)
{
area = area + (x[i + 1] - x[i]) * (y[i] + y[i + 1]) / 2;
}
}
else
{
printf("%s: Error, incorrect arguments provided\n",argv[0]);
return 2;
}
printf("x: %4.2f y: %4.2f \n", x[count], y[count]);
printf("%f \n", area);
}
fclose(fptr);
return 0;
}
calculates area under the curve using either Simpsons rule or the
Trapezium rule.
The contents of the text file could be:
5
0.00 1.00
0.50 0.75
1.00 2.00
1.50 4.75
2.00 9.00
The program is called with 2 arguments, the text file
and then S or T to specify a rule */
#include <stdio.h>
#include <varargs.h>
#include <math.h>
int main(int argc, char*argv[])
{
int i;
int n = 0;
float x[5];
float y[5];
float area = 0;
short count;
FILE *fptr;
char rule;
if (argc < 3)
{
printf("%s: Error, incorrect arguments provided\n",argv[0]);
return 1;
}
if ((fptr = fopen(argv[1], "r")) == NULL)
{
printf("Error opening file\n");
return 3;
}
rule = *argv[2];
fscanf(fptr, "%d", &n);
for (count = 0; count < 5; count++)
{
fscanf(fptr, "%f %f", &x[count], &y[count]);
}
for (count = 0; count < 5; count++)
{
if (rule == 'S') {
for (i = 0; i <= n - 3; i = i + 2)
{
area = area + (x[i + 1] - x[i]) * (y[i] + (4 * y[i + 1]) + y[i + 2]) / 3;
}
}
else if (rule == 'T') {
for (i = 0; i <= n - 2; i++)
{
area = area + (x[i + 1] - x[i]) * (y[i] + y[i + 1]) / 2;
}
}
else
{
printf("%s: Error, incorrect arguments provided\n",argv[0]);
return 2;
}
printf("x: %4.2f y: %4.2f \n", x[count], y[count]);
printf("%f \n", area);
}
fclose(fptr);
return 0;
}
wordguess__user_guide.docx | |
File Size: | 283 kb |
File Type: | docx |
#include <stdio.h>
#include <varargs.h>
#include <string.h>
#include <stdlib.h>
#define FALSE 0
#define TRUE 1
int numeric(char *character)
{
int valid;
valid = TRUE;
if (*character < '0' || *character > '9')
{
valid = FALSE;
}
return valid;
}
int main(int argc, char*argv[])
{
char c;
FILE *fptr;
short word_count = 0;
int asteriskCount = 0;
short in_word = FALSE;
int chars_in_word = 0;
int randNum;
char chosenWord[11];
char asterisks[11];
int i;
int guesses;
char letterGuess[2];
if (argc != 3)
{
printf("%s: incorrect amount of arguments\n", argv[0]);
return 1;
}
//call numeric check on number of guesses
if ((numeric(argv[2])) == FALSE)
{
printf("Number of guesses not an integer");
return 2;
}
//check if file can be opened
if ((fptr = fopen(argv[1], "r")) == NULL)
{
printf("%s: Error opening file '%s' \n", argv[0], argv[1]);
perror("");
return 3;
}
//count words from file
while ((c = fgetc(fptr)) != EOF)
{
if (c != ' ' && c != '\n')
{
if (!in_word)
{
word_count++;
in_word = TRUE;
}
}
else
{
in_word = FALSE;
}
}
//close file
if ((fclose(fptr)) == EOF)
{
printf("%s: Error closing file '%s'\n", argv[0], argv[1]);
perror("");
return 4;
}
//use word count to get random
randNum = rand() % word_count;
//open file and go to chosen word
word_count = 0;
i = 0;
fptr = fopen(argv[1], "r");
rewind(fptr);
while ((c = fgetc(fptr)) != EOF)
{
if (word_count != randNum)
{
if (c == ' ' || c == '\n')
{
word_count++;
}
}
else
{
if (c != ' ' && c != '\n')
{
chosenWord[i] = c;
chars_in_word++;
}
else
{
chosenWord[chars_in_word] = '\0';
break;
}
i++;
}
}
//close file
if ((fclose(fptr)) == EOF)
{
printf("%s: Error closing file '%s'\n", argv[0], argv[1]);
perror("");
return 4;
}
//create array of asterisks and print
for (i = 0; i < chars_in_word; i++)
{
asterisks[i] = '*';
}
asterisks[chars_in_word] = '\0';
printf("\n %s\n", asterisks);
//take guesses
guesses = atoi(argv[2]);
while (guesses > 0)
{
printf("\n guess a letter: ");
while ((letterGuess[0] = getchar()) != '\n' && letterGuess[0] != EOF)
{
if (toupper(letterGuess[0]) < 'A' || toupper(letterGuess[0]) > 'Z')
{
printf("\n invalid input\n");
}
else
{
for (i = 0; i < chars_in_word; i++)
{
if (chosenWord[i] == letterGuess[0])
{
asterisks[i] = letterGuess[0];
printf("\n %s \n", asterisks);
asteriskCount++;
}
}
guesses = guesses - 1;
}
}
if (asteriskCount == chars_in_word)
{
printf("\n Well Done");
return 5;
}
else
{
continue;
}
}
printf("\n Hard Luck");
return 6;
}