Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please use C++ Jason, Samantha, Ravi, Sheila, and Ankit are preparing for an upc

ID: 3573549 • Letter: P

Question

Please use C++

Jason, Samantha, Ravi, Sheila, and Ankit are preparing for an upcoming marathon. Each day of the week, they run a certain number of miles and write them into a notebook. At the end of the week, they would like to know the number of miles run each day, the total miles for the week, and average miles run each day. Write a program to help them analyze their data. Your program must contain vectors: an vector to store the names of the runners and a two-dimensional array of five rows and seven columns to store the number of miles run by each runner each day. Furthermore, your program must contain at least the following functions: a function to read and store the runners’ names and the numbers of miles run each day; a function to find the total miles run by each runner and the average number of miles run each day; and a function to output the results. (Your may assume that the input data is stored in a file and each line of data is in the following form: runnerName milesDay1 milesDay2 milesDay3 milesDay4 milesDay5 milesDay6 milesDay7.)

The program should be able to accommodate any number of runners.

Explanation / Answer

import java.util.*;
import java.io.*;

class MarathonRecord
{
public static void main( String [] args ) throws Exception
{
int[][] miles = new int[5][7];
Vector v = new Vector(5,1);
int c=-1;
  
  
Scanner sc= new Scanner(System.in);
  
raedDetails(v, miles);
while(c!=0)
{
System.out.println("Enter 1 to save record, 2 to display all records, 3 to display record of a runner or 0 to exit");
c=sc.nextInt();
  
if(c==1)
{
v.removeAllElements();
saveRecord();
raedDetails(v, miles);

}
else if(c==2)
dispDetails(v,miles);
else if(c==3)
searchRunner(v, miles);
else if(c==0) break;
  
}
  
  
  
  
  
  
  
}
  
public static void saveRecord()
{
String name, line="";
try
{
BufferedWriter br = new BufferedWriter(new FileWriter("results.txt", true));
br.newLine();
Scanner sc= new Scanner(System.in);
System.out.println("Enter the name of runner ");
name= sc.nextLine();
System.out.println("Enter miles run each day ");
  
line+=name+" "+sc.next()+" "+sc.next()+" "+sc.next()+" "+sc.next()+" "+sc.next()+" "+sc.next()+" "+sc.next();
  
br.write(line);
br.close();
}
catch(Exception ex)
{
System.out.println(ex);
  
}
}
  
public static void raedDetails(Vector v, int miles[][])
{
try
{
Scanner sc= new Scanner(new File("results.txt"));
  
String name;
int total=0,i=0;   
  
while(sc.hasNextLine())
{
v.addElement(new String(sc.next()));
for(int j=0; j<7;j++)
{
miles[i][j]= sc.nextInt();
}
i++;
}
}
catch(Exception e)
{
System.out.println(e);
}

}
public static void dispDetails(Vector v, int a[][])
{
int s = v.size();
int total;
String name;
for(int i=0; i<s;i++)
{
total=0;
name=(String)v.get(i);
for(int j=0;j<7;j++)
{
total+=a[i][j];
  
}
System.out.println(name+" has run "+total+" miles in a weak with an average of "+total/7+" miles a day!");
}
}

public static void searchRunner(Vector v, int a[][])
{
  
int total;
String name,n;
boolean f=false;
Scanner sc= new Scanner(System.in);
System.out.println("Enter name to search");
name=sc.nextLine();
for(int i=0; i<v.size();i++)
{
total=0;
n=(String)v.get(i);
if(name.equalsIgnoreCase(n))
{
f=true;
for(int j=0;j<7;j++)
{
total+=a[i][j];

}
System.out.println(name+" has run "+total+" miles in a weak with an average of "+total/7+" miles a day!");
break;
}

  
}
if(!f) System.out.println(name+" not found");

}
  
  

}