By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,655 Members | 959 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,655 IT Pros & Developers. It's quick & easy.

Simple Program using Arrays

P: n/a
I am a newbie to C++ and I need help with a simple program I am trying
to write. My program is designed to print all of the odd integers
contained in an array and output the sum of the odd integers. My code
is listed below, but the problem is that my output of sum is wrong.
For example, I am using 1347830 for my integers and the program outputs
373 and after adding 373 you should get 13 for the total, but that
doesn't happen. Any help you can provide is appreciated! Thanks!
------------------------------------------------------------*---
#include <iostream.h>
#include <iomanip.h>

void main()
{
char numbers[20];
int sum = 0;

cout << "Barcode: ";
cin.get(numbers, 20);

for(int i = 1; i < numbers[i]; i+=2)
{
cout << numbers[i];
sum += numbers[i];
}
cout << endl;
cout << sum;
cout << endl;
}

Jul 23 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
ho************@hotmail.com wrote:
I am a newbie to C++ and I need help with a simple program I am trying
to write. My program is designed to print all of the odd integers
contained in an array and output the sum of the odd integers. My code
is listed below, but the problem is that my output of sum is wrong.
For example, I am using 1347830 for my integers and the program outputs
373 and after adding 373 you should get 13 for the total, but that
doesn't happen. Any help you can provide is appreciated! Thanks!
------------------------------------------------------------*---
#include <iostream.h>
#include <iomanip.h>

void main()
{
char numbers[20];
int sum = 0;

cout << "Barcode: ";
cin.get(numbers, 20);

for(int i = 1; i < numbers[i]; i+=2)
{
cout << numbers[i];
sum += numbers[i];
}


I think you should write i < 20 instead of i < numbers[i]. More
precisely i < (number of elements you actually put in the array...)

Bye

Andrea
Jul 23 '05 #2

P: n/a
ho************@hotmail.com wrote:
I am a newbie to C++ and I need help with a simple program I am trying
to write. My program is designed to print all of the odd integers
contained in an array and output the sum of the odd integers. My code
is listed below, but the problem is that my output of sum is wrong.
For example, I am using 1347830 for my integers and the program outputs
373 and after adding 373 you should get 13 for the total, but that
doesn't happen. Any help you can provide is appreciated! Thanks!
------------------------------------------------------------*---
#include <iostream.h>
#include <iomanip.h>

void main()
{
char numbers[20];
int sum = 0;

cout << "Barcode: ";
cin.get(numbers, 20);

for(int i = 1; i < numbers[i]; i+=2)
{
cout << numbers[i];
sum += numbers[i];
}
cout << endl;
cout << sum;
cout << endl;
}


Note that arrays in C/C++ start at index 0. That means, the first odd
number would be at [0], the next at [2] and so on.
Jul 23 '05 #3

P: n/a

<ho************@hotmail.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...
I am a newbie to C++ and I need help with a simple program I am trying
to write. My program is designed to print all of the odd integers
contained in an array
Question: why did you define your array as containing
type 'char' values, and not type 'int'? Besides the
other major problems I point out below, this issue
alone will give you wrong results (explained below).
and output the sum of the odd integers. My code
is listed below, but the problem is that my output of sum is wrong.
For example, I am using 1347830
I'll have to assume that you mean the sequence of integers
1,3,4,7,8,3,0
for my integers and the program outputs 373
You should separate your values so we don't confuse
3,7,3 with the integer 373
and after adding 373
There's another odd integer in your sequence: 1
you should get 13 for the total,
My brain computes 14. :-)
but that
doesn't happen. Any help you can provide is appreciated! Thanks!


#include <iostream.h>

#include <iostream>
#include <ostream>

#include <iomanip.h>

#include <iomanip>
/* but you're not using anything from this
header anyway, you don't need it */

using namespace std;

void main()

int main()

/* main() returns type 'int'. ALWAYS */

{
char numbers[20];
int sum = 0;

cout << "Barcode: ";
cin.get(numbers, 20);

Better would be:

cin.get(numbers, sizeof numbers);

Even better would be to use a std::string (declared by <string>)
instead of an array:

std::string numbers;
std::getline(cin, numbers);
/* (you can address the characters of a std::string using
the same syntax as for an array of characters) */

for(int i = 1; i < numbers[i]; i+=2)

All three expressions in your 'for' are wrong:

1. Arrays in C++ are indexed beginning with zero, not one.

2. Why are you comparing your counter against an array element?
(also note that comparing against any which have not been
assigned a value produces undefined behavior -- which
will happen if the user enters less than 19 characters
-- this problem could have been avoided had you initialized
your array when you created it.)

3. Why are you skipping every other element in your array?
This simply accesses the elements whose indices are odd
numbers. How can you check every element for 'oddness'
if you don't examine all of them?
{
cout << numbers[i];
sum += numbers[i];
}

Your loop simply accumulates the sum of all the elements
whose indices are odd numbers. It doesn't check the values
themselves for 'oddness' at all. And again, if any of
your array elements haven't been given values, trying
to access them gives undefined behavior. Again, initializing
your array would have prevented this problem. You need to
determine the actual number of characters which were
input, and use that as your limit value in your loop
(see function 'strlen()', declared by <cstring> or
string.h, or if using 'std::string', see its member
function 'size()')

The promised 'explanation' mentioned above:
Once you get all the rest of these problems sorted out,
note that when you assign an input character to a
type 'char' object, what gets stored is the encoded value
for that character as defined by the character set your
computer uses (e.g. ASCII). On on ASCII system, character
'5', has a numeric value of 53, not 5.

The possible solutions are:
1:
Define your array as having elements of type 'int', and
request each value with a separate input (these can still
be supplied by the user on one line, but must be separated
by whitespace). (This will let you use values other than
0 - 9). You'll need to check for and reject bad input (i.e.
containing non-digit characters).

2:
Keep your array as having 'char' objects (or use a std::string),
and translate the digit characters to their corresponding
numeric values. This can be done portably by subtracting
the value '0' (the character literal, not the integer literal 0)
from each. Again, you'll need to also check each character to
ensure it's a digit.
cout << endl;
cout << sum;
cout << endl;
}

If you don't understand some or all of what I wrote above, feel
free to ask specific questions.

HTH,
-Mike

Jul 23 '05 #4

P: n/a
Based on what I'm seeing here, I'm guessing you've been skipping out until
now. Am I right? And midterms are just around the bend. Sheesh.

<ho************@hotmail.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...
I am a newbie to C++ and I need help with a simple program I am trying
to write. My program is designed to print all of the odd integers
contained in an array and output the sum of the odd integers. My code
is listed below, but the problem is that my output of sum is wrong.
For example, I am using 1347830 for my integers and the program outputs
373 and after adding 373 you should get 13 for the total, but that
doesn't happen. Any help you can provide is appreciated! Thanks!
------------------------------------------------------------*---
#include <iostream.h>
#include <iomanip.h>

void main()
{
char numbers[20];
int sum = 0;

cout << "Barcode: ";
cin.get(numbers, 20);

for(int i = 1; i < numbers[i]; i+=2)
{
cout << numbers[i];
sum += numbers[i];
}
cout << endl;
cout << sum;
cout << endl;
}
Jul 23 '05 #5

P: n/a
<ho************@hotmail.com> wrote:

I am a newbie to C++ and I need help with a simple program I am trying
to write. My program is designed to print all of the odd integers
contained in an array and output the sum of the odd integers. My code
is listed below, but the problem is that my output of sum is wrong.
For example, I am using 1347830 for my integers and the program outputs
373 and after adding 373 you should get 13 for the total, but that
doesn't happen. Any help you can provide is appreciated! Thanks!
------------------------------------------------------------*---
#include <iostream.h>
#include <iomanip.h>

void main()
{
char numbers[20];
int sum = 0;

cout << "Barcode: ";
cin.get(numbers, 20);

for(int i = 1; i < numbers[i]; i+=2)
{
cout << numbers[i];
sum += numbers[i];
}
cout << endl;
cout << sum;
cout << endl;
}

Are you sure you understand what is to be done? I think you are supposed to
find the odd *digits*. You are only examining half the digits, IOW you are
looking at odd *locations*. (the i+= 2 code) Look at all the locations and
determine if the digit is odd or even. Use the modulo operator(%) to make
your decision.

Look up the words digit and integer in a dictionary; they are not the same
thing.

I admit there is a certain amount of guesswork in my answer. Because, in
the C world, an array of char does not contain any integers. There is a
messy bit about the distinction between things and the representation of
those things. And I am *postulating* what seems to me a reasonable
assignment in a course on C.
Jul 23 '05 #6

P: n/a
"osmium" <r1********@comcast.net> wrote in message
news:36*************@individual.net...
<ho************@hotmail.com> wrote:

I am a newbie to C++ and I need help with a simple program I am trying
to write. My program is designed to print all of the odd integers
contained in an array and output the sum of the odd integers. My code
is listed below, but the problem is that my output of sum is wrong.
For example, I am using 1347830 for my integers and the program outputs
373 and after adding 373 you should get 13 for the total, but that
doesn't happen. Any help you can provide is appreciated! Thanks!
------------------------------------------------------------*---
#include <iostream.h>
#include <iomanip.h>

void main()
{
char numbers[20];
int sum = 0;

cout << "Barcode: ";
cin.get(numbers, 20);

for(int i = 1; i < numbers[i]; i+=2)
{
cout << numbers[i];
sum += numbers[i];
}
cout << endl;
cout << sum;
cout << endl;
}

Are you sure you understand what is to be done? I think you are supposed
to find the odd *digits*. You are only examining half the digits, IOW you
are looking at odd *locations*. (the i+= 2 code) Look at all the
locations and determine if the digit is odd or even. Use the modulo
operator(%) to make your decision.

Look up the words digit and integer in a dictionary; they are not the same
thing.

I admit there is a certain amount of guesswork in my answer. Because, in
the C world, an array of char does not contain any integers. There is a
messy bit about the distinction between things and the representation of
those things. And I am *postulating* what seems to me a reasonable
assignment in a course on C.


I don't know what happened to the corner brackets in that post - they seem
to have disappeared.

-- Osmium.
Jul 23 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.