Hi!
On before hand, thanks for any help! I have written this code but it won't compile. It has been converted from Java to C++ so there might be some traces of Java in there that I'm just not able to catch. This was the original assignment:
Write a program that generates some basic statistics on a set of input values. The program begins by first prompting the user to specify the number of values, say n, that are to be processed. Once this value has been determined program should prompt the user to enter n values, with one prompt per value to be entered.
For each of the n values entered, the program should perform the following actions (without performing any additional output per value:
• Maintain a running total value
• Determine if the value is negative
• Determine if the value is an integer
• If the value is an integer, determine whether it is even or odd
After all of the values have been input to the program, the program should then output the following statistics concerning the input:
• The number of values entered
• The sum of the values entered
• The average of the values entered
• The number of negative values entered
• The number of even values entered
• The number of odd values entered
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
#include <iostream>
using namespace std;
public class ine_prog {
/**
* @param args
*/
private static void operations(String s) {
//check if number is double
if (s.indexOf('.') > 0) {
numberOfDoubles++;
}
//check if number is positive
else if (integer.parseInt(s) > 0) {
numberOfIntegers++;
if (integer.parseInt(s) % 2 == 0) {
numberOfEvens++;
}
else {
numberOfOdds++;
}
}
else if (double.parseDouble(s) < 0) {
numberOfNegatives++;
//totalSum += Double.parseDouble(s);
}
totalSum += double.parseDouble(s);
}
private static void statistics() {
cout<<"\n\nNumber of values entered: " + numberOfValues + "\n";
cout << "Sum of values entered: " + totalSum + "\n";
cout << "The average of the values entered: " + (double) (totalSum / numberOfValues) + "\n";
cout << "Number of negative values entered: " + numberOfNegatives + "\n";
cout << "Number of even values entered: " + numberOfEvens + "\n";
cout << "Number of odd values entered: " + numberOfOdds + "\n";
cout << "Number of integer values entered: " + numberOfIntegers + "\n";
cout << "Number of double values entered: " + numberOfDoubles + "\n";
}
static int numberOfValues = 0;
static double totalSum = 0;
static int numberOfNegatives = 0;
static int numberOfEvens = 0;
static int numberOfOdds = 0;
static int numberOfDoubles = 0;
static int numberOfIntegers = 0;
public static void main(String [] args) {
Scanner in = new Scanner(System.in);
cout <<("Enter number of values:\n");
numberOfValues = in.nextInt();
if (numberOfValues > 0) {
for (int i = 0; i < numberOfValues; i++) {
cout<<"Enter value: " + "\n";
cin>> in.next();
operations(s); //call this function for every value entered
}
statistics(); //present statistics by calling this function
}
}
}
|