Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In this section you will learn how to use a struct to store a fraction. This will let us make calculations without ever needing to

In this section you will learn how to use a struct to store a fraction. This will let us make calculations without ever needing to store the floating decimal. Storing the numerator and denominator as individual integers has a few benefits. For example, it allows use to store the exact fraction of an irrational number, there are fewer memory requirements, no need for converting datatypes, and no rounding errors (rounding errors are common even with floating point numbers).

Make sure to always put the fraction in lowest terms before returning the result or your answer will be incorrect.You will be provided with the GCD (greatest common divisor) function. The GCD will find the largest divisor of two integers so you only need to divide each by the gcd to put both into lowest terms.

======

ADD CODE TO THIS

#include "lab04.h" /* Finds the absolute value of an integer */ int absoluteValue(int value) { if (value < 0) { value = -value; } return value; } /* This function is used for returning a new fraction */ Fraction newFraction(int num, int denom) { Fraction new; new.numerator = num; new.denominator = denom; return new; } /* Finds the greatest common divisor between two integers */ int GCD(int one, int two) { /* GCD == Greatest Common Divisor */ int smallest; /* Get absolute value */ one = absoluteValue(one); two = absoluteValue(two); /* Get smallest starting value */ if (one < two) { smallest = one; } else { smallest = two; } /* Continue to negatively iterate until finding the gcd */ while(smallest > 1) { /* Find an integer where the remainder is 0 */ if (one%smallest == 0 && two%smallest == 0) { return smallest; } smallest--; } return smallest; } /* Simple function to print the content of a fraction */ void printFraction(Fraction p) { printf("%d/%d ", p.numerator, p.denominator); } /* Returns the integer percentage of a fraction. It does not round the result */ int percentage(Fraction frac) { int result; result = (frac.numerator*100)/frac.denominator; return result; } /*** * Functions for you to complete ***/ /* This function finds a common denominator and scales the numerator and denominator of both fractions. */ void commonDenominator(Fraction *one, Fraction *two) { /* Hint: you do not need to add/subtract in lowest terms. You can convert to lowest after */ } /* Returns a fraction in the lowest terms possible from the fraction passed */ Fraction lowestTerms(Fraction toLower) { /* Hint: Used gcd to find the greatest common divisor */ Fraction inLowest; return inLowest; } /* Adds fraction one and two together */ Fraction add(Fraction fracOne, Fraction fracTwo) { Fraction result; return result; } /* Subtracts two fractions (one - two) */ Fraction subtract(Fraction fracOne, Fraction fracTwo) { Fraction result; return result; } /* Multiplies fractions together */ Fraction multiply(Fraction one, Fraction two) { Fraction result; return result; } /* Fraction one divided by fraction two */ Fraction divide(Fraction one, Fraction two) { /* Hint: you may want to use a temp variable when flipping fraction two */ Fraction result; return result; }

=========

TEST CASE

include "lab04.h" int main(int argc, char *argv[]) { Fraction one = newFraction(1, 3); Fraction two = newFraction(4, 7); Fraction three = newFraction(2, 3); Fraction four = newFraction(2, 15); printf(" ***** Print Original Fractions ***** "); printf("1 (%d%%): ", percentage(one)); printFraction(one); printf("2 (%d%%): ", percentage(two)); printFraction(two); printf("3 (%d%%): ", percentage(three)); printFraction(three); printf("4 (%d%%): ", percentage(four)); printFraction(four); /* Testing Fraction operations, you can add whatever you want but this testing is done for you */ /* Uncomment the code as functionality progresses */ /*printf(" ***** Testing Add ***** "); printf("Should be 19/21: "); printFraction(add(one, two)); printf("Should be 4/5: "); printFraction(add(three, four)); printf(" ***** Testing Subtract ***** "); printf("Should be -5/21: "); printFraction(subtract(one, two)); printf("Should be 8/15: "); printFraction(subtract(three, four)); printf(" ***** Testing Multiply ***** "); printf("Should be 4/21: "); printFraction(multiply(one, two)); printf("Should be 4/45: "); printFraction(multiply(three, four)); printf(" ***** Testing Divide ***** "); printf("Should be 7/12: "); printFraction(divide(one, two)); printf("Should be 5/1: "); printFraction(divide(three, four));*/ return 0; }

==============

In this section of the lab we will write functions for processing an array of structs and allocate memory for a string within each struct stored. Complete the functions below.

Instruction:

Write functions for finding the best and worst grade in a stack array.

Find the average mark of the arrays.

ADD CODE TO THIS....

include "lab04.h" /*Returns a new student with from the passed arguments */ StudentGrade newStudentGrade(char *studentName, int numerator, int denominator) { /* Hint 1: If you are confused about the purpose of this function look at newFraction */ /* Hint 2: Do not forget to null terminate your string */ StudentGrade toReturn; return toReturn; } /* Entire class average as a percentage */ int classAverage(StudentGrade *classList, int length) { /* Hint: use the percentage function in the fractions.c file */ int avg; return avg; } /* Find the top mark in an array of grades (top mark is a percentage) */ int topMark(StudentGrade *classList, int length) { /* Hint: Remember to use the percentage function */ int current; return current; } /* Find the lowest mark in an array of grades (lowest mark is a percentage) */ int lowestMark(StudentGrade *classList, int length) { /* Hint: Remember to use the percentage function */ int current; return current; }

TEST CASE

#include "lab04.h" void printFraction(Fraction p); int main(int argc, char *argv[]) { StudentGrade classList[5]; classList[0] = newStudentGrade("First Last", 12, 20); classList[1] = newStudentGrade("name NaMe", 19, 20); classList[2] = newStudentGrade("foo bar", 20, 20); classList[3] = newStudentGrade("food Bar", 1, 20); classList[4] = newStudentGrade("Bar Food ", 15, 20); /* For testing you can just use a random number generator */ /*Uncomment code when you ready and add any testing you want */ /*printf("Average = %d%% (=67%%) ", classAverage(classList, 5)); printf("Top Mark = %d%% (=100%%) ", topMark(classList, 5)); printf("Lowest Mark = %d%% (=5%%) ", lowestMark(classList, 5));*/ return 0; } -------

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design For Mere Mortals

Authors: Michael J Hernandez

4th Edition

978-0136788041

More Books

Students also viewed these Databases questions

Question

Sell the quality of your brand or products.

Answered: 1 week ago