Simple Program using Arrays

H

hoover_richard

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+=2)
{
cout << numbers;
sum += numbers;
}
cout << endl;
cout << sum;
cout << endl;
}
 
A

Andrea Sini

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+=2)
{
cout << numbers;
sum += numbers;
}


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

Bye

Andrea
 
K

Karsten Baumgarten

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+=2)
{
cout << numbers;
sum += numbers;
}
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.
 
M

Mike Wahler

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+=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;
sum += numbers;
}

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
 
D

deancoo

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.

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+=2)
{
cout << numbers;
sum += numbers;
}
cout << endl;
cout << sum;
cout << endl;
}
 
O

osmium

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+=2)
{
cout << numbers;
sum += numbers;
}
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.
 
O

osmium

osmium said:
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+=2)
{
cout << numbers;
sum += numbers;
}
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,201
Messages
2,571,048
Members
47,650
Latest member
IanTylor5

Latest Threads

Top