×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

count in array

count in array

count in array

(OP)
Can anyone tell me what is wrong with this program?

#include <iostream.h>
int main()
{
  int count[]= {0};
  int ch, x;
  float num , total, average;

  x =0;
  total = 0.0;

  for (count[x] =0; count[x] < ch; count[x]++)
  {
cout << "Enter a number:";
cin>> count[ch];
total = total + count[ch];
  }
  average = total / 6;
  cout<<average<<endl;

  return 0;
}

RE: count in array

joebeach,

    Just a quick look I can see a couple of things that stand out about your for loop. First, ch is not initialized. When you type int ch, you are setting aside memory for the variable but you are not setting it to anything. The value of ch is going to be whatever was already at that memory location unless you set it to something.

    Second, your for loop is saying that you are checking the contents of the array to see if it is less than ch. I think that you need to be checking the variable x and not count[x].

    Try this:
#include <iostream.h>
int main()
{
    int ch = 6;
    int count[ch]= {0};
    float total, average;

    total = 0.0;

    for (int x = 0; x < ch; x++)
    {
        cout << "Enter a number:";
        cin>> count[ch];
        total = total + count[ch];
    }

    average = total / ch;
    cout<<average<<endl;

    return 0;
}

    I haven't run this to check it but it seems to me that this is more like what you want.

James P. Cottingham
main@ivcusa.com
International Veneer Co., Inc.
All opinions are mine alone and do not necessarily reflect those of my employer.

RE: count in array

(OP)
2ffat,
The only thing is that ch has to be an unknow count because the user has to input how many time.  It should be dymanically.  like one time it would be 2.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close