SlideShare a Scribd company logo
1 of 3
Download to read offline
Need help with this C program. I have it almsot all completed. Option 1 asks the user to enter a
fraction and then option 2 displays that fraction. option 3 sorts the fractions in ascending order, i
figured out all the code for this however for option 4 i need to Calculate the Max, Min and
Median fractions. I can't figure out what the code for it is. If the user selects option 4 before
sorting the fractions, then a "Please sort the values first" should pop up.
#include
#include
//Struct to hold fraction data
typedef struct fraction
{
int numerator, denom;
}fraction;
double Calc_Frac(fraction b)
{
return((double)b.numerator / b.denom);
}
int main()
{ fraction arrFraction[100];
int i = 0;
int j;
int num = 1;
while (num == 1)
{
int choice;
printf(" Press 1 to enter a fraction ");
printf("Press 2 to view stored fractions ");
printf("Press 3 to sort fractions ");
printf("Press 4 to find min max median fraction ");
scanf("%d", &choice);
if(choice == 1)
{
//Prompting user
printf(" Enter your fraction, numerator followed by denominator ");
//Reading values from user
scanf("%d %d", &arrFraction[i].numerator, &arrFraction[i].denom);
//Incrementing counter
i++;
}
if (choice == 2) {
printf("------------------------- ");
for (j = 0; j < i; j++)
{
printf("%d %d/%d  ", arrFraction[j].numerator/arrFraction[j].denom,
arrFraction[j].numerator%arrFraction[j].denom, arrFraction[j].denom);
}
printf(" -------------------------  ");
}
if (choice == 3) {
int min;
fraction tmp;
for (int k = 0; k < i; k++)
{
min = k;
for (j = k + 1; j < i; j++)
{
if (Calc_Frac(arrFraction[j]) < Calc_Frac(arrFraction[min]))
{
min = j;
}
}
tmp = arrFraction[k];
arrFraction[k] = arrFraction[min];
arrFraction[min] = tmp;
}
}
if (choice == 4)
{
}
}
system("pause");
return(0);
}
Solution
if (choice == 4)
{
if(chosen3==1)
{
printf("Min fraction: %d/%d",arrFraction[0].numerator,arrFraction[0].denom);
printf("Max fraction: %d/%d",arrFraction[i-1].numerator,arrFraction[i-1].denom);
printf("Median fraction: %d/%d",arrFraction[i/2].numerator,arrFraction[i/2].denom);
}
else
{
printf("Please sort the values first");
}
If array is already sorted,then min will be at index 0
max would be at last index
median will be at middle

More Related Content

Similar to Need help with this C program. I have it almsot all completed. Optio.pdf

I am constantly getting errors and cannot figure this out. Please he.pdf
I am constantly getting errors and cannot figure this out. Please he.pdfI am constantly getting errors and cannot figure this out. Please he.pdf
I am constantly getting errors and cannot figure this out. Please he.pdf
allystraders
 
Chapter 3 - Variable Memory Concept
Chapter 3 - Variable Memory ConceptChapter 3 - Variable Memory Concept
Chapter 3 - Variable Memory Concept
Deepak Singh
 
Digits.javapackage week04;import java.util.Scanner; public cla.pdf
Digits.javapackage week04;import java.util.Scanner; public cla.pdfDigits.javapackage week04;import java.util.Scanner; public cla.pdf
Digits.javapackage week04;import java.util.Scanner; public cla.pdf
annapurnnatextailes
 
I worte the code according to the requirement.And also I wrote the c.pdf
I worte the code according to the requirement.And also I wrote the c.pdfI worte the code according to the requirement.And also I wrote the c.pdf
I worte the code according to the requirement.And also I wrote the c.pdf
sudhinjv
 
week3_srcDoWhileLoopFactorial.javaweek3_srcDoWhileLoopFactoria.docx
week3_srcDoWhileLoopFactorial.javaweek3_srcDoWhileLoopFactoria.docxweek3_srcDoWhileLoopFactorial.javaweek3_srcDoWhileLoopFactoria.docx
week3_srcDoWhileLoopFactorial.javaweek3_srcDoWhileLoopFactoria.docx
alanfhall8953
 
Task #1 Correcting Logic Errors in FormulasCopy and compile the so.pdf
Task #1 Correcting Logic Errors in FormulasCopy and compile the so.pdfTask #1 Correcting Logic Errors in FormulasCopy and compile the so.pdf
Task #1 Correcting Logic Errors in FormulasCopy and compile the so.pdf
info706022
 
1584503386 1st chap
1584503386 1st chap1584503386 1st chap
1584503386 1st chap
thuhiendtk4
 
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docxPROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
amrit47
 

Similar to Need help with this C program. I have it almsot all completed. Optio.pdf (20)

I am constantly getting errors and cannot figure this out. Please he.pdf
I am constantly getting errors and cannot figure this out. Please he.pdfI am constantly getting errors and cannot figure this out. Please he.pdf
I am constantly getting errors and cannot figure this out. Please he.pdf
 
Conditional Statements
Conditional StatementsConditional Statements
Conditional Statements
 
Programming basics
Programming basicsProgramming basics
Programming basics
 
Embedded C - Lecture 2
Embedded C - Lecture 2Embedded C - Lecture 2
Embedded C - Lecture 2
 
C and data structure
C and data structureC and data structure
C and data structure
 
The language is C. Thanks. Write a function that computes the intege.pdf
The language is C. Thanks. Write a function that computes the intege.pdfThe language is C. Thanks. Write a function that computes the intege.pdf
The language is C. Thanks. Write a function that computes the intege.pdf
 
C chap02
C chap02C chap02
C chap02
 
C chap02
C chap02C chap02
C chap02
 
Control statements-Computer programming
Control statements-Computer programmingControl statements-Computer programming
Control statements-Computer programming
 
01 list using array
01 list using array01 list using array
01 list using array
 
Chapter 3 - Variable Memory Concept
Chapter 3 - Variable Memory ConceptChapter 3 - Variable Memory Concept
Chapter 3 - Variable Memory Concept
 
Digits.javapackage week04;import java.util.Scanner; public cla.pdf
Digits.javapackage week04;import java.util.Scanner; public cla.pdfDigits.javapackage week04;import java.util.Scanner; public cla.pdf
Digits.javapackage week04;import java.util.Scanner; public cla.pdf
 
I worte the code according to the requirement.And also I wrote the c.pdf
I worte the code according to the requirement.And also I wrote the c.pdfI worte the code according to the requirement.And also I wrote the c.pdf
I worte the code according to the requirement.And also I wrote the c.pdf
 
week3_srcDoWhileLoopFactorial.javaweek3_srcDoWhileLoopFactoria.docx
week3_srcDoWhileLoopFactorial.javaweek3_srcDoWhileLoopFactoria.docxweek3_srcDoWhileLoopFactorial.javaweek3_srcDoWhileLoopFactoria.docx
week3_srcDoWhileLoopFactorial.javaweek3_srcDoWhileLoopFactoria.docx
 
Relational Operators in C
Relational Operators in CRelational Operators in C
Relational Operators in C
 
Task #1 Correcting Logic Errors in FormulasCopy and compile the so.pdf
Task #1 Correcting Logic Errors in FormulasCopy and compile the so.pdfTask #1 Correcting Logic Errors in FormulasCopy and compile the so.pdf
Task #1 Correcting Logic Errors in FormulasCopy and compile the so.pdf
 
1584503386 1st chap
1584503386 1st chap1584503386 1st chap
1584503386 1st chap
 
C programming Lab 2
C programming Lab 2C programming Lab 2
C programming Lab 2
 
UNIT-II CP DOC.docx
UNIT-II CP DOC.docxUNIT-II CP DOC.docx
UNIT-II CP DOC.docx
 
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docxPROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
 

More from eyezoneamritsar

How visual information is sent from the eye to the rest of the brain.pdf
How visual information is sent from the eye to the rest of the brain.pdfHow visual information is sent from the eye to the rest of the brain.pdf
How visual information is sent from the eye to the rest of the brain.pdf
eyezoneamritsar
 
Describe the major interactions among organisms in a food web.So.pdf
Describe the major interactions among organisms in a food web.So.pdfDescribe the major interactions among organisms in a food web.So.pdf
Describe the major interactions among organisms in a food web.So.pdf
eyezoneamritsar
 
Why are tourist destinations attempting to attract more visitors out.pdf
Why are tourist destinations attempting to attract more visitors out.pdfWhy are tourist destinations attempting to attract more visitors out.pdf
Why are tourist destinations attempting to attract more visitors out.pdf
eyezoneamritsar
 
High School Blues Evan and Alexia had been happily married for seven.pdf
High School Blues Evan and Alexia had been happily married for seven.pdfHigh School Blues Evan and Alexia had been happily married for seven.pdf
High School Blues Evan and Alexia had been happily married for seven.pdf
eyezoneamritsar
 
C# I need assitance on my code.. Im getting an error message Us.pdf
C# I need assitance on my code.. Im getting an error message Us.pdfC# I need assitance on my code.. Im getting an error message Us.pdf
C# I need assitance on my code.. Im getting an error message Us.pdf
eyezoneamritsar
 

More from eyezoneamritsar (20)

Identify 3 cell components that are shared in common between prokary.pdf
Identify 3 cell components that are shared in common between prokary.pdfIdentify 3 cell components that are shared in common between prokary.pdf
Identify 3 cell components that are shared in common between prokary.pdf
 
How many pairs of wires are in a standard Ethernet cableDefend or.pdf
How many pairs of wires are in a standard Ethernet cableDefend or.pdfHow many pairs of wires are in a standard Ethernet cableDefend or.pdf
How many pairs of wires are in a standard Ethernet cableDefend or.pdf
 
Fst can be used to measure the number of migrants per gegenetration .pdf
Fst can be used to measure the number of migrants per gegenetration .pdfFst can be used to measure the number of migrants per gegenetration .pdf
Fst can be used to measure the number of migrants per gegenetration .pdf
 
How visual information is sent from the eye to the rest of the brain.pdf
How visual information is sent from the eye to the rest of the brain.pdfHow visual information is sent from the eye to the rest of the brain.pdf
How visual information is sent from the eye to the rest of the brain.pdf
 
For coherently demodulated BPSK, what should EbN0 be in dB so as to.pdf
For coherently demodulated BPSK, what should EbN0 be in dB so as to.pdfFor coherently demodulated BPSK, what should EbN0 be in dB so as to.pdf
For coherently demodulated BPSK, what should EbN0 be in dB so as to.pdf
 
Cisco Systems The Supply Chain Story11. Study the networked suppl.pdf
Cisco Systems The Supply Chain Story11. Study the networked suppl.pdfCisco Systems The Supply Chain Story11. Study the networked suppl.pdf
Cisco Systems The Supply Chain Story11. Study the networked suppl.pdf
 
Describe four threats to high data availability and at least one mea.pdf
Describe four threats to high data availability and at least one mea.pdfDescribe four threats to high data availability and at least one mea.pdf
Describe four threats to high data availability and at least one mea.pdf
 
Describe the major interactions among organisms in a food web.So.pdf
Describe the major interactions among organisms in a food web.So.pdfDescribe the major interactions among organisms in a food web.So.pdf
Describe the major interactions among organisms in a food web.So.pdf
 
Consider the following circle of radius 5 centered at the origin. Fi.pdf
Consider the following circle of radius 5 centered at the origin.  Fi.pdfConsider the following circle of radius 5 centered at the origin.  Fi.pdf
Consider the following circle of radius 5 centered at the origin. Fi.pdf
 
Write one Conditional Signal Assignment VHDL code statement in the b.pdf
Write one Conditional Signal Assignment VHDL code statement in the b.pdfWrite one Conditional Signal Assignment VHDL code statement in the b.pdf
Write one Conditional Signal Assignment VHDL code statement in the b.pdf
 
Why are tourist destinations attempting to attract more visitors out.pdf
Why are tourist destinations attempting to attract more visitors out.pdfWhy are tourist destinations attempting to attract more visitors out.pdf
Why are tourist destinations attempting to attract more visitors out.pdf
 
Which pair of words are synonymsA ephemeral--evanescentB euphon.pdf
Which pair of words are synonymsA ephemeral--evanescentB euphon.pdfWhich pair of words are synonymsA ephemeral--evanescentB euphon.pdf
Which pair of words are synonymsA ephemeral--evanescentB euphon.pdf
 
Which of the following embryonic materials is matched with a correct.pdf
Which of the following embryonic materials is matched with a correct.pdfWhich of the following embryonic materials is matched with a correct.pdf
Which of the following embryonic materials is matched with a correct.pdf
 
We used 3 files with each lab project in Vivado. What do we call the.pdf
We used 3 files with each lab project in Vivado.  What do we call the.pdfWe used 3 files with each lab project in Vivado.  What do we call the.pdf
We used 3 files with each lab project in Vivado. What do we call the.pdf
 
To determine the distance between short and black on chromosome II th.pdf
To determine the distance between short and black on chromosome II th.pdfTo determine the distance between short and black on chromosome II th.pdf
To determine the distance between short and black on chromosome II th.pdf
 
High School Blues Evan and Alexia had been happily married for seven.pdf
High School Blues Evan and Alexia had been happily married for seven.pdfHigh School Blues Evan and Alexia had been happily married for seven.pdf
High School Blues Evan and Alexia had been happily married for seven.pdf
 
Suppose within your Web browser, you click on a link to obtain a Webp.pdf
Suppose within your Web browser, you click on a link to obtain a Webp.pdfSuppose within your Web browser, you click on a link to obtain a Webp.pdf
Suppose within your Web browser, you click on a link to obtain a Webp.pdf
 
Supports cukaryotic cellsSolutionEukaryotes are the organisms h.pdf
Supports cukaryotic cellsSolutionEukaryotes are the organisms h.pdfSupports cukaryotic cellsSolutionEukaryotes are the organisms h.pdf
Supports cukaryotic cellsSolutionEukaryotes are the organisms h.pdf
 
C# I need assitance on my code.. Im getting an error message Us.pdf
C# I need assitance on my code.. Im getting an error message Us.pdfC# I need assitance on my code.. Im getting an error message Us.pdf
C# I need assitance on my code.. Im getting an error message Us.pdf
 
Briefly define and describe what IASB and IFRS are.SolutionIAS.pdf
Briefly define and describe what IASB and IFRS are.SolutionIAS.pdfBriefly define and describe what IASB and IFRS are.SolutionIAS.pdf
Briefly define and describe what IASB and IFRS are.SolutionIAS.pdf
 

Recently uploaded

Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 

Recently uploaded (20)

REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 

Need help with this C program. I have it almsot all completed. Optio.pdf

  • 1. Need help with this C program. I have it almsot all completed. Option 1 asks the user to enter a fraction and then option 2 displays that fraction. option 3 sorts the fractions in ascending order, i figured out all the code for this however for option 4 i need to Calculate the Max, Min and Median fractions. I can't figure out what the code for it is. If the user selects option 4 before sorting the fractions, then a "Please sort the values first" should pop up. #include #include //Struct to hold fraction data typedef struct fraction { int numerator, denom; }fraction; double Calc_Frac(fraction b) { return((double)b.numerator / b.denom); } int main() { fraction arrFraction[100]; int i = 0; int j; int num = 1; while (num == 1) { int choice; printf(" Press 1 to enter a fraction "); printf("Press 2 to view stored fractions "); printf("Press 3 to sort fractions "); printf("Press 4 to find min max median fraction "); scanf("%d", &choice); if(choice == 1) { //Prompting user
  • 2. printf(" Enter your fraction, numerator followed by denominator "); //Reading values from user scanf("%d %d", &arrFraction[i].numerator, &arrFraction[i].denom); //Incrementing counter i++; } if (choice == 2) { printf("------------------------- "); for (j = 0; j < i; j++) { printf("%d %d/%d ", arrFraction[j].numerator/arrFraction[j].denom, arrFraction[j].numerator%arrFraction[j].denom, arrFraction[j].denom); } printf(" ------------------------- "); } if (choice == 3) { int min; fraction tmp; for (int k = 0; k < i; k++) { min = k; for (j = k + 1; j < i; j++) { if (Calc_Frac(arrFraction[j]) < Calc_Frac(arrFraction[min])) { min = j; } } tmp = arrFraction[k]; arrFraction[k] = arrFraction[min]; arrFraction[min] = tmp;
  • 3. } } if (choice == 4) { } } system("pause"); return(0); } Solution if (choice == 4) { if(chosen3==1) { printf("Min fraction: %d/%d",arrFraction[0].numerator,arrFraction[0].denom); printf("Max fraction: %d/%d",arrFraction[i-1].numerator,arrFraction[i-1].denom); printf("Median fraction: %d/%d",arrFraction[i/2].numerator,arrFraction[i/2].denom); } else { printf("Please sort the values first"); } If array is already sorted,then min will be at index 0 max would be at last index median will be at middle