In: Computer Science
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);
}
#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
*/