so I'm learning java and I'm trying to write a program that prompts the user for exam scores (values 1-100), and when the user enter some negative number to end the entering sequence, it will then print out the number of scores entered, the highest score, the lowest score, and the average of all the scores.
I'm being challenged to do this with simple comparison statements and loops, and WITHOUT using arrays.
I'm kinda stumped here.
This is what I have so far.
import java.util.*;
import java.text.DecimalFormat;
public class Lab3
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
DecimalFormat df = new DecimalFormat("0.00");
String input;
double examScores;
System.out.println("Exam score: ");
input = sc.nextLine();
examScores = Double.parseDouble(input);
}
}
>>58781348
>java
best advice is just install gentoo
>>58781361
what
>>58781348
You have no loops, come back when you actually try.
>>58781451
import java.util.*;
import java.text.DecimalFormat;
public class Lab3
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
DecimalFormat df = new DecimalFormat("0.00");
String input;
double examScores;
double highScore = 0.00;
double lowScore = 101.00;
double averageScore;
int numInput = 0;
do
{
System.out.println("Enter exam score (1-100), or enter a negative number to end: ");
input = sc.nextLine();
examScores = Double.parseDouble(input);
if (examScores < lowScore)
{
examScores = lowScore;
}
else if (examScores > highScore)
{
examScores = highScore;
}
numInput++;
} while (examScores >= 0);
}
}
>>58781348
Alright OP, I can tell you're a complete newfag but everyone needs to start somewhere so here's what you do.
>kys
Now that we got that out of the way, here:
int count=0;
double highest = 0.0;
double lowest = 101.0;
double avg = 0;
double input=0;
while(true){
input = Double.valueOf(sc.NextLine());
if (input<0)
break;
if (input>highest)
highest=input;
if(input<lowest)
lowest=input
if (count != 0){
avg = (avg * count/(count+1.0)) + input * (1/(count+1.0));
count++;
} else {
avg =input;
}
}
There.
Future advice, think of how you'd program this in pseudocode before you attempt it at all.
Also make more of a damn effort next time.
>>58781732
Motherfucker this fucks up on negatives. On a negative this loop will iterate at least once.
>>58781732
This shit doesn't even handle the average faggot, jesus.
>>58781840
Where is it fucking up on negs?
>>58781851
I was getting there.
#include <stdlib.h>
#include <iostream>
#include <vector>
Here you go OP! All you had to do was use the vector class :)
int main()
{
int input;
int highest_score = 1;
int lowest_score = 100;
int average = 0;
std::vector<int> scores;
while (std::cin >> input && input > 0)
{
scores.push_back(input);
if (input > highest_score)
{
highest_score = input;
}
if (input < lowest_score)
{
lowest_score = input;
}
}
std::cout << "\nHighest Score: " << highest_score;
std::cout << "\nLowest Score: " << lowest_score;
for (int i = 0; i < scores.size(); i++)
{
average += scores[i];
}
std::cout << "\nAverage Score: " << average / scores.size();
std::cout << "\nAll Scores:\n";
for (int i = 0; i < scores.size(); i++)
{
std::cout << scores[i] << "\n";
}
std::cin >> input;//consolefreeze
}
>>58781865
You don't check for negatives in the loop body, the loop will always run at least once, even if the input is negative.
>>58781986
Java.
Jaaaaaaaaavaaaaaaa.
also he said no arrays.
>>58782061
kek, yeah I was just fucking around and I don't feel like opening netbeans
>>58781818
So I'm using your method modified a little bit but not in a way to ruin it, but at the end when I print out the values I want, it prints the negative number entered to end the program as the Low Score.
>>58782094
Sounds like you didn't actually copy the body of the loop then
>input = Double.valueOf(sc.NextLine());
>if (input<0)
>break;
This being at the top of the while loop stops the loop from executing when the input is negative. If this is there then it is actually impossible for the lowest to get set to negative.
>>58782133
To add onto this.
The average would be off too, it just isn't as apparent because the method I used only partially adjusts the average each time.
>>58782133
>>58782151
So is that the same as
if (input < 0)
{
System.exit(0);
}
??
>>58781818
why is your average function so strange?
you could just say
if (count < 1)
{
avg = input;
}
else
{
avg = (avg + input) / 2;
}
>>58782193
>avg = (avg + input) / 2;
Should be:
avg =(avg + input)/count + 1;
>>58782169
No, but if you're really new I can see why you might think that
break; just exits the loop, not the program.
System.exit(0) will exit the program itself, you'll normally see that in the catch block of a try{}catch(){} which is something you probably haven't learned yet, so just think of System.exit() only applying to fatal errors.
break; is a reserved word and just exits loops, (for, while and do).
It's also used in switch statements but that's its own case.
>>58782229
no m8, it's recursive
look at my attached pic
>>58782232
>>58782232
this is essentially my first real programming class ever
>>58782252
to explain further what you're essentially doing is getting the average of the first two values, then using your that average and the next input you get a new average and you continue this process until the program ends
>>58782229
yeah this would work too.
My average function basically made it a sum of fractions, your's reads better but they effectively do the same thing.
My way of thinking of a running average is that the average after multiple inputs is the equivalent of having every single entry in the set be that average, so when you add some new entry it would be the equivalent of:
((average * count) + new entry) / (count + 1)
so when I wrote it out on one line it looked like:
(average * count) / (count+1) + (new entry)/(count+1)
That was always easier for me to memorize.
>>58782257
Yeah no worries dude. Java is a fine starting language and teaches fundamentals very well.
/g/ hates Java though so...
Some advice:
It's worth it to ask questions and read ahead. Stackoverflow is your friend.
Knowing programming humor will get you in any programmer's good graces quickly enough.
The user is a fucking retard, ALWAYS.
C is basically Latin.
A recurring problem among programmers is that we all push our favorite language too hard. Learn what you're taught for the first few years, use the first class that changes from Java to REALLY learn how to master a new language and quickly.]
You'll always know Java better than any other language because it was your first.
Do you guys not understand how programming and averages work?
This is literally the easiest formula there is.
>>58782229
>>58782308
I hope nobody thinks this shit is right. You're just proving the meme that CompScis can't do math.
>tfw tested my own formula and it was wrong
fucking math
>>58782193
haha jk I was right
the negative was bringing my number down at the end
phew, thought I lost my common sense for a second there
if (count < 1)
{
avg = input;
}
else if (input > 0)
{
avg = (avg + input) / 2;
}
>>58782467
Well let's fucking test mine then.
We'll go ahead and do numbers 1-10.
start with 1.
2:
((1*1) + 2)/2 = 1.5
3:
((2*1.5)+3)/3 = 2
4:
((3*2)+4)/4 = 2.5
5:
((4*2.5)+5)/5 = 3
etc.
We end up with 5, which is correct.
Maybe next time you should fucking test it before calling it wrong.
>>58782843
Your function fails for the following:
10,10,10,10,15
Correct average: 11
Your function: 12.5
>>58782398
Is this a troll?
[math]\frac{6+7+9}{3} \neq \frac{ \frac{6+7}{2} +9}{2}[/math]
>>58782869
err, 5.5
Still correct. (I forgot I wasn't including 0).
>>58782899
that's because you're using integers, try using float or double
>>58782869
why would you do unnecessary calculations?
>>58782899
Oh wait you're right
>>58782978
Why would using integers even change anything?
>>58783163
with integer division you lose accuracy because of the rounding down that happens in programming ( 9/2 = 4)
if you divide enough integers by non-products you can lose a lot of information on the way to your answer
that's why when you want precision you use float or double
>>58782869
that's not equal to the function you wrote if this is you >>58782229
that's another function
appears to work though
avg =(avg + input)/count + 1;
use code tags stupid niger
>>58781348
Jesus fucking christ you don't even need arrays
Java should be compatible with C.#define MAX 100
#define MIN 1
#define UNUSED 0
#define EXIT_SUCCESS 45
int main (void){
float avg=UNUSED;
int counts=UNUSED;
int highest=MIN;
int lowest=MAX;
int input=UNUSED;
while(input>=UNUSED){
//get input
puts("Enter your score:");
scanf("%d", &input);
//if the input is between the limits
if (input<=MAX && input >= MIN){
//determine if it's the max or min
if (input>=highest){
highest=input;
}
if (input<=lowest){
lowest=input;
}
//reset the avg
avg=(avg+input)/2;
//keep count
counts++;
}
}
printf("%s%8d.\n", "The Highest score is", highest);
printf("%s%8d.\n", "The Lowest score is", lowest);
printf("%s%8d.\n", "The number of scroes is", counts);
printf("%s%8.2f.\n", "The average of the scores are", avg);
return EXIT_SUCCESS;
}
R8/H8/Masterb8.
If it solves your problem, solve mine now:
#write a program that outputs the stream input directly with multiple spaces deducted.stdin: this is a test.
stdout: this is a test.
>>58784097
Oh and it works too
>>58784097#include <stdio.h>
int main()
{
int c, state;
state = 0;
while((c = getchar()) != EOF){
if(c == ' '){
if(state == 0){
state = 1;
putchar(c);
}
} else if (c != ' ') {
state = 0;
putchar(c);
}
}
return 0;
}[16:28 1008]$ echo "this is a test" | 1-9
this is a test
[16:28 1009]$
>>58784158
Thanks
>>58782398
what the fuck
>>58784237
Not him but he is right. This is a simple way to find average and update the avg variable.
See what >>58784097 did.
>>58784264
How is he right? ((1 + 2)/2 + 3)/2 does not give you the mean of 1, 2, and 3.
>>58784305
avg = 0
input = 2
new avg =(0+2)/2 //=1
new avg = 1
new input = 3
new avg = (1+3)/2 //=2
avg keeps updating
so does the input
keyword: (((update)))
>>58784305
>((1 + 2)/2 + 3)/2
That's not what he said
>>58784097
>avg=(avg+input)/2;
For this to work you have to assigns avg to inout for the first time
avg of 15 is 15, not 7.5
>>58784359
>((1 + 2)/2 + 3)/2
his image says (1 + 2 + 3)/2 = ((1 + 2)/2 + 3)/2
>>58784347
that does not give you the average of all the inputs
>>58784398
No his image says (((1+2)/2)+3)/2
>>58784385
Ops such a silly mistake.
Here.#include <stdio.h>
#define MAX 100
#define MIN 1
#define UNUSED 0
#define EXIT_SUCCESS 45
int main (void){
float avg=UNUSED;
int counts=UNUSED;
int highest=MIN;
int lowest=MAX;
int input=UNUSED;
while(input>=UNUSED){
//get input
puts("Enter your score:");
scanf("%d", &input);
//if the input is between the limits
if (input<=MAX && input >= MIN){
//determine if it's the max or min
if (input>=highest){
highest=input;
}
if (input<=lowest){
lowest=input;
}
//reset the avg
if (counts==UNUSED)
avg=input;
else
avg=(avg+input)/2;
//keep count
counts++;
}
}
printf("%-s%8d.\n", "The Highest score is", highest);
printf("%-s%8d.\n", "The Lowest score is", lowest);
printf("%-s%8d.\n", "The number of scroes is", counts);
printf("%-s%8.2f.\n", "The average of the scores are", avg);
return EXIT_SUCCESS;
}
>>58784436
>
>>58784398
>(1 + 2 + 3)/2 = ((1 + 2)/2 + 3)/2
It says (a + b + c)/3 = ((a + b)/2 + c)/2, which is still fucking wrong.
>>58784516
I didn't re-compile then
>>58784568
Do those answers make sense to you? Confirmed troll thread. I'm out.
>>58784541
> ((a + b)/2 + c)/2
No it doesn't
it says (a+b)/2 + ((a+b)/2+c)/2 + ...
Which is objectively correct
>>58784541
>>58784415
>>58784398
Fucking retards
(a+b+c+d)/2 = (a+b)/2 + (c+d)/2
Absolutely kill yourselves
wow how did this turn into a debate over mathematics
>tfw this thread could've been resolved easily if not for the fucking average function
>>58785806
my sides
>>58784670
Hello, it is the guy who made the shitty paint equations. I've come to redeem myself.
You are indeed correct, but that doesn't give you the full equation
Let (a + b) / 2 = g (g is the average of a and b)
Now let us imagine an additional quantity c
Our sum now follows (a + b + c) / 3, but we have lost a and b and we are only left with c, the number of quantities we have, and g
However, we know that g = (a + b)/2 therefore 2g = (a + b)
And since all we need to do is ((2g + c) / 3) or (((a +b) + c) / 3)
A way to write this in pseudocode would be:
(((numberofquantities - 1) * oldaverage) + newquantity) / numberofquantities
I have become mentally stronger today. Thanks /g/.
>>58786813
just kill yourself you fucking moron
Why don't you fucks just keep a running total of all the scores combined and increment amount of scores entered then divide at the end?
Wastes less of muh pentium 2 cpu time as well.
>>58781442
download moar ram