Question

In: Computer Science

In main, create two FracList objects, ask the user how many elements to allocate for each...

In main, create two FracList objects, ask the user how many elements to allocate for each list, read as many Fraction objects from the keyboard as specified by the user into each (using >> operator). Sort both lists using the sort member function and display them; and then search for a value read from the user in both lists and print the index of the first occurrence or that it could not be found in either list.

Swap the two lists entered by the user so that the smaller size list (using < or > operator which only compares their size) is the first and larger size is the second. Use a swap function to swap the two lists. The example given in module did not use a function. Remember to swap two objects, you must either pass them by reference, or pass their addresses into couple of pointers. Print both lists again to verify the swap.

Resize the larger list to a size one larger than its original size, keeping all its values and assigning to its last element the sum of all other fractions (after reducing it), and print again.

Example run of the program:

Enter size of first fraction list: 5

Enter size of second fraction list: 3

Enter 5 fractions for first list: 1/4 1/8 2/3 1/3 1/2

Enter 3 fractions for second list: 3/4 1/5 1/4

Sorted lists:

1/8 1/4 1/3 1/2 2/3

1/5 1/4 3/4

Enter a fraction to search for: 1/4

1/4 was found at index 1 of list 1.

Swapped lists:

1/5 1/4 3/4

1/8 1/4 1/3 1/2 2/3

Resized list with sum of fractions at the end:

1/8 1/4 1/3 1/2 2/3 15/8

Press any key to continue.

Use separate files for class definitions (.h), function definitions and main program (3 files).

typedef struct fraction

{

int numerator, denom;

}fraction;

int main()
{
//Array of 100 fractions
fraction arrFraction[100];

int i = 0;

int j;

int num = 1;

while (num == 1)
{

int choice;

printf("\nPress 1 to enter a fraction\n");

printf("Press 2 to view stored fractions\n");

printf("Press 3 to sort fractions\n");

scanf("%d", &choice);
if(choice == 1)
{
//Prompting user
printf("\nEnter your fraction, numerator followed by denominator\n");

//Reading values from user
scanf("%d %d",&arrFraction[i].numerator, &arrFraction[i].denom);
//Incrementing counter
i++;
}
if (choice == 2) {
printf("-------------------------\n");
for (j = 0; j < i; j++)
{
printf("%d %d/%d \n", arrFraction[j].numerator/arrFraction[j].denom,
arrFraction[j].numerator%arrFraction[j].denom, arrFraction[j].denom);
}
printf("\n-------------------------\n\n");
}

if (choice == 3)
{

int min;
fraction tmp;

for (i = 0; i < j; i++)
{
min = i;
for (j = i + 1; j < i; j++)
{
if (Calc_Frac(arrFraction[j]) < Calc_Frac(arrFraction[min]))
{
min = j;
}
}
tmp = arrFraction[i];
arrFraction[i] = arrFraction[min];
arrFraction[min] = tmp;
}
i++;
}

}


}
return(0);
}

Solutions

Expert Solution

#include< bits/stdc++.h>

using namespace std;

struct fraction {

int num , den ;

};

fraction maxfraction(fraction first , fraction sec)

{

int a= first.num;

int b = first.den;

int c = sec. num;

int d= sec.den;

int y = a*b - b*c;

return (y>0)? first : sec:

}

int main()

{

fraction first ={ 3,2};

fraction sec ={ 3,4};

fraction res = maxfraction(first,sec);

cout<< res.num<<"/"<< res.den;

return 0;

}

// C code to take input and sort fraction
#include <stdio.h>
#include <stdlib.h>

//Struct to hold fraction data
typedef struct fraction
{
int numerator, denom;
}fraction;

int main()
{
//Array of 100 fractions
fraction arrFraction[100];
int i = 0;
int j;
int k;
int num = 1;
  
while (num == 1)
{
int choice;
printf("\nPress 1 to enter a fraction\n");
printf("Press 2 to view stored fractions\n");
printf("Press 3 to sort fractions\n");
printf("Press 4 to Quit\n");
scanf("%d", &choice);

if(choice == 1)
{
//Prompting user
printf("\nEnter your fraction, numerator followed by denominator\n");
//Reading values from user
scanf("%d %d", &arrFraction[i].numerator, &arrFraction[i].denom);
//Incrementing counter
i++;
  
}
else if (choice == 2) {
printf("-------------------------\n");
for (j = 0; j < i; j++)
  
{
printf("%f %d/%d \n", (float)arrFraction[j].numerator/arrFraction[j].denom, arrFraction[j].numerator%arrFraction[j].denom, arrFraction[j].denom);
}
printf("\n-------------------------\n\n");
}
  
  
  
else if (choice == 3)
{
  
for (j = 0; j < i; j++)
{
for (k = 0; k < i; k++)
{
float fraction1 = (float)arrFraction[j].numerator/arrFraction[j].denom;
float fraction2 = (float)arrFraction[k].numerator/arrFraction[k].denom;

if(fraction2 > fraction1)
{
int temp1 = arrFraction[j].numerator;
arrFraction[j].numerator = arrFraction[k].numerator;
arrFraction[k].numerator = temp1;

int temp2 = arrFraction[j].denom;
arrFraction[j].denom = arrFraction[k].denom;
arrFraction[k].denom = temp2;

}
}
}

printf("Fraction Sorted\n");

}

else if (choice == 4)
break;

else
printf("Invalid Input\n");
  
}
return(0);
}

/*
output:


Press 1 to enter a fraction
Press 2 to view stored fractions
Press 3 to sort fractions
Press 4 to Quit
1

Enter your fraction, numerator followed by denominator

5
6

Press 1 to enter a fraction
Press 2 to view stored fractions
Press 3 to sort fractions
Press 4 to Quit
1

Enter your fraction, numerator followed by denominator
1
2

Press 1 to enter a fraction
Press 2 to view stored fractions
Press 3 to sort fractions
Press 4 to Quit
1

Enter your fraction, numerator followed by denominator
3
4

Press 1 to enter a fraction
Press 2 to view stored fractions
Press 3 to sort fractions
Press 4 to Quit
1

Enter your fraction, numerator followed by denominator
9
10

Press 1 to enter a fraction
Press 2 to view stored fractions
Press 3 to sort fractions
Press 4 to Quit
2
-------------------------
0.833333 5/6
0.500000 1/2
0.750000 3/4
0.900000 9/10

-------------------------


Press 1 to enter a fraction
Press 2 to view stored fractions
Press 3 to sort fractions
Press 4 to Quit
3
Fractions Sorted


Press 1 to enter a fraction
Press 2 to view stored fractions
Press 3 to sort fractions
Press 4 to Quit
2
-------------------------
0.500000 1/2
0.750000 3/4
0.833333 5/6
0.900000 9/10

-------------------------


Press 1 to enter a fraction
Press 2 to view stored fractions
Press 3 to sort fractions
Press 4 to Quit
4


*/


Related Solutions

Create a C++ program that will ask the user for how many test scores will be...
Create a C++ program that will ask the user for how many test scores will be entered. Setup a while loop with this loop iteration parameter. (no fstream) The data needs to include the student’s first name, student number test score the fields should be displayed with a total width of 15. The prompt should be printed with a header in the file explaining what each is: ex. First Name student number Test Score 1) mike 6456464   98 2) phill...
C LANGUAGE Ask user how many scores there are, then ask for each score. Then calculate...
C LANGUAGE Ask user how many scores there are, then ask for each score. Then calculate the average score and scores below 60. Then display the average score and number of scores below 60
You have been asked to create a python program that will ask the user how many...
You have been asked to create a python program that will ask the user how many tickets the user would like to purchase for graduation. The user will then choose the number of tickets needed and the program will list the cost of the tickets, a service fee, tax, and the total of the purchase. Specifications: The program should only accept whole numbers when receiving input for the number of tickets needed. No calculations should be performed if input is...
Q1) Please create a C++ program that will ask the user for how many test scores...
Q1) Please create a C++ program that will ask the user for how many test scores will be entered. Setup a while loop with this loop iteration parameter. The data will include the student’s first name, Wayne State Access ID, midterm score and their favorite team (i.e. – Doug, hg1702, 92, Wolverines * - John, hv2201, 99, Warriors). Print out the completed test scores to a file (midTermScores.txt) . Adjust the output as follows: (15 points) a. Scores with the...
ARDUINO Create a function that will ask the user to enter the values for each of...
ARDUINO Create a function that will ask the user to enter the values for each of the three colors (red, green and blue) between 0 and 255. The function should check that the user does not enter a number bigger than 255 or smaller than 0. If this happens, the function should keep asking for a valid number.
Create a program (Python) YourFirstnameLastnameA06b.py to ask the user to create a password: The user will...
Create a program (Python) YourFirstnameLastnameA06b.py to ask the user to create a password: The user will first enter a password, then enters the same password again; If the second input is the same as first one, the user successfully creates the password. Print “Well done.”; Otherwise, the user will be directed to repeat the whole process (go to step 1.)
Java Codes: 1. Create a class named Proficiency1Practice In the main method, first ask the user...
Java Codes: 1. Create a class named Proficiency1Practice In the main method, first ask the user to enter a short sentence which ends in a period. Use Java String class methods to determine the following about the sentence and display in the console: • How many total characters are in the sentence? • What is the first word of the sentence? Assume the words are separated by a space. • How many characters are in the first word of the...
Create a small program that contains the following. ask the user to input their name ask...
Create a small program that contains the following. ask the user to input their name ask the user to input three numbers check if their first number is between their second and third numbers
C++ program that will ask the user for how many test scores will be entered. Setup...
C++ program that will ask the user for how many test scores will be entered. Setup a while loop with this loop iteration parameter. The data will include the student’s first name and midterm score Print out the completed test scores to a file (midTermScores.txt) . Print out list of students names and grades in the print out, a letter grade should replace numeric score using standard grading (a = 90 – 100, b=80-90, c=70-80, d=60-70, f=below 60)
Write a program in Java to: Ask the user how many scores they want to enter...
Write a program in Java to: Ask the user how many scores they want to enter (=> Create an array based the entered size) Ask the user to enter their scores in the quarter (Fill the array with the entered scores(assigned to elements)) ==>> Use a "for" loop Find the greatest score Calculate and show the average Show the final grade based on the average (For example A,B,C ... ) Print the scores (the elements of the array) => Use...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT