Whats wrong with my code? I wanna get the summation of the array , C++
Go to solution
Solved by Eigenvektor,
Please, post code as text in a code block… makes it much easier for people to try your code. If you're asking what the problem is, it's also usually a good idea to tell people if it just doesn't behave as expected (and what's happening instead) or if you're getting a compiler error (which one?)
At a quick glance, your array contains doubles, but your sum total is an int. Either change your sum to a double, or round your values to int before trying to add them.
~edit: Here's a working example
Spoiler
#include <iostream> int main() { double prices[] = { 6.99, 8.5, 42.9, 105.4, 42, 98.1, }; int size = sizeof(prices) / sizeof(double); double total = 0; for (int i = 0; i < size; i++) { total += prices[i]; } std::cout << "sum of " << size << " numbers is: " << total << std::endl; }
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now