Beginner\'s Luck Filename: luck.c Please consult the ORIGINAL PDF posted here: h
ID: 3671409 • Letter: B
Question
Beginner's Luck
Filename: luck.c
Please consult the ORIGINAL PDF posted here:
http://www.cs.ucf.edu/courses/cop3502/spr2016/sparta/quiz-problems/Luck.pdf
It always seems that beginners have amazing luck. Just last week you showed your friend Liliana a video game that you had been playing for over a year. On her first try, her score was better than your average score!!!
Now, you and Liliana have gotten into a discussion about whether beginners really are lucky. Luckily, you have data about many individuals playing the video game. For each individual, you'd like to make a claim of the form:
In their first X games, [Insert Name Here] scored an average of Y points per game.
To make the claim as impressive as possible, you'd like to maximize the value of Y, given the player's full game history, over all possible positive integers for X.
For example, if Michael had gotten the following scores (in this order):
10, 8, 9, 15, 12, 2, 3, 8, 7, 11
we see that the best claim we can make is that in the first 5 games, Michael averaged a score of (10 + 8 + 9 + 15 + 12)/5 = 10.8 points per game. (Note that each of the running averages, rounded to one decimal place are 10.0, 9.0, 9.0, 10.5, 10.8, 9.3, 8.4, 8.4, 8.2, and 8.5, respectively.)
The Problem:
Given a list of positive integers, determine the maximum average of any non-empty prefix of the list.
The Input:
The first line of input contains a single positive integer, n (n 100), the number of input cases to be processed. Each input cases follows, one per line. For each input case, the first number is a positive integer, k (k 1000000), representing the number of times the player for the input case has played the video game. The following k space-separated integers represent the player's scores for each time playing the video game, in order. Each of these integers will be in between 0 and 1000, inclusive.
The Output:
For each test case, on a line by itself, output the maximal average of any of the first m (1 m k) games, rounded to 6 decimal places.
Sample Input:
2
10
8
9
15
12
2
3
8
7
11
3
3
2
1
Sample Output:
10.800000
3.000000
Explanation / Answer
Answer:
//Luck.c
#include<stdio.h>
#include<stdlib.h>
float maxAvgFn(int *intArry, int n, int k)
{
int k1;
int cumSUM=0;
for(k1=0;k1<k;k1++)
{
cumSUM+=intArry[k1];
}
return (float)cumSUM/k;
}
int main()
{
int c;
int s1;
int intArry[15];
int k1,k2,k3;
int n ;
float maxAvg=1;
float t;
scanf("%d",&c);
for(k1=0;k1<c;k1++)
{
scanf("%d",&s1);
for(k2=0;k2<s1;k2++)
{
scanf("%d",&intArry[k2]);
}
maxAvg=1;
for(k3=0;k3<s1-1;k3++)
{
t=maxAvgFn(intArry, s1, k3);
if(t>maxAvg)
maxAvg=t;
}
printf(" MaxAverage :%.6f" , maxAvg);
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.