Array task(dont get logic)

Hello!
I just finished learning about very basics in C++ using lynda.com and next chapter is arrays. I got this task there, which I don't understand.
Maybe there is someone who would like to write something of this task and comment code or just explain to me.

Problem is, I don't understand logic of this program.I know how to repeat program with out leaving it, but that wont help me to create main function :D
I am thinking to start UNI in computer science next year, but until then I want to learn something by myself, so its easier for me to succeed there.


Write C++ program in which user has to enter whole number array. Create new array in which are recorded every number digit No.
It should be possible to execute the program again without leaving the program.

Also I would appreciate some tips and maybe websites where I can practice C++(where are tasks and I am writing program step by step,I saw something similar for Java)

Thanks
Hello weathergood,

You might start with reading this about arrays http://www.cplusplus.com/doc/tutorial/arrays/

A suggestion for defining the array:

1
2
3
4
5
6
7
8
9
10
#include <iostream>

int main()
{
	constexpr std::size_t MAXSIZE{ 100 }; // <--- or you could use "100U"

	int numbers[MAXSIZE]{};

	return 0;
}

An explanation.
"constexpr" is the newer form of "const", C++11 on. Read on it at https://en.cppreference.com/w/cpp/language/constexpr

In simple terms you can think of "size_t" as another name for an "unsigned in". Actually it is a "typedef".

When defining a constant variable name capital letters are used to remind you that it is a constant variable.

The {}s are used in two ways: first to initialize "MAXSIZE" to 100. Second to initialize the entire array to zero.

The purpose of "MAXSIZE" is that you can use it anywhere in "main" and only have one place to change its size if needed. Then everywhere it is used will change when the program is compiled.

I would guess that you would use a for loop or while loop to make the entries. Defining a variable like "count" or "amtUsed" will let you know how much of the array is actually used. This way when dealing with the array you will only process the part that is used and not the whole array.

Hope that helps,

Andy

P.S. The link https://en.cppreference.com/w/ is a good place to find information about key words and functions in C++. A good link to bookmark for future use.
Write C++ program in which user has to enter whole number array.
Create new array in which are recorded every number digit No.
It should be possible to execute the program again without leaving the program.

The first line sounds simple: "read numbers from user (std::cin)".
What does "record number digit No" mean?


The third line is a separate task that does not relate to the arrays in any way.
Lets say you have got a program done that does lines 1-2 once and it looks like:
1
2
3
4
5
int main()
{
  // many
  // statements
}

You want to "execute" the
1
2
  // many
  // statements 

multiple times before the program ends. That is called a loop.
See http://www.cplusplus.com/doc/tutorial/control/

Adapted from example:
1
2
3
4
5
6
7
8
9
10
int main()
{
  std::string str;
  do {
    // many
    // statements

    getline( std::cin, str );
  } while ( str != "goodbye" );
}

Now the user has to enter something after the "many statements" and if it is not "goodbye", then things repeat.
Topic archived. No new replies allowed.