I am working on one-dimensional arrays for class assignment. I don't think I have this right. Can someone check this:
The question is: Write the c++ code that will count the number of elements in an array (named number) of base type int. that are equal to a key value named key_value. The number of components is saved in the variable: size.

here is what I got:
int number[] = key_value;
int size = sizeof (number)/sizeof (number[0];

how far off am I?

nah. You got the assigment wrong.
You start with an array of ints. for example:

int number[10] = {1,2,3,4,1,6,7,8,9,1};

Now you will have to write a program to see how often the number '1' appears in the array.
The easiest way to do this is to make a loop that goes through every element of the array (so from number[0] to number[9]) and looks if the element equals the key_value.
So you'll need a loop and a if-statement.

ps. This is actually more a C then a C++ question.

I think that you need is this: The comments I made are things you need to do to complete the assignment.

const int size = 10; // or whatever number you want
int number[size] = {0}; // initialize all elements to 0
int key_value = 0;
// initialize all elements of the array with some numbers
// of your choice.

// ask and prompt for key_value so that you can
// type it from the keyboard

// create a loop to check each element of the array
// for key_value.

[edit]Didn't see Nick's post above [/edit]

Edited 5 Years Ago by Nick Evan: n/a

thanks, but now I need to create a loop? to get the key_value with a number (like 1?) and send them to the variable size. how do I do that

I think that you need is this: The comments I made are things you need to do to complete the assignment.

const int size = 10; // or whatever number you want
int number[size] = {0}; // initialize all elements to 0
int key_value = 0;
// initialize all elements of the array with some numbers
// of your choice.

// ask and prompt for key_value so that you can
// type it from the keyboard

// create a loop to check each element of the array
// for key_value.

[edit]Didn't see Niek's post above [/edit]

thanks for helping but I am still not understanding this

// initialize all elements of the array with some numbers
// of your choice.

That means to set the value of each element of the array to some numbers. There are several ways to do it but the simplest way is like in nick's post above int number[10] = {1,2,3,4,1,6,7,8,9,1}; That means that
number[0] = 1
number[1] = 2
number[3] = 3
number[4] = 4
number[5] = 1
// etc. etc
You can set those values to anything you want -- choice is up to you.

// ask and prompt for key_value so that you can
// type it from the keyboard

display a prompt that tells the user that he/she is to enter a number

cout << "Enter a key value\n";
cin >> key_value;

// create a loop to check each element of the array
// for key_value

Here you have to know about loops. There are a number of tutorials on the net that show you how to code loops. Here is just one of many

In the body of the loop you want to create an if statement to check the value of numbers with key_value. In below code variable i is the loop counter.

if( key_value == numbers[i] )
// do something here

Edited 5 Years Ago by Nick Evan: n/a

This article has been dead for over six months. Start a new discussion instead.