C++ Newbie

Hi there fellow reader!

I bought my first C++ book about a year ago, I'm not done with it, been on/off about reading it, but in recent time I've started to read alot more C++ not just the book but also the Cplusplus.com Documentation Here's where I'm at http://www.cplusplus.com/doc/tutorial/inheritance/.
Tho I've some issues I can't get things to stick in me brain.

Here's a list of issues I'm having, I welcome any suggestion: On how to use them in code, learning exactly what they are and do and hoppefully make them stick in my personal memory bank(Brain).

[list]
[*]Pointers
[*]Classes
[*]Arrays
[/list]

Reading the guide CplusPlus documentation and examine the code and write the code doesn't seem to work for me to evolve in my C++ learning curve and I've gone through the same chapter about pointers/class and arrays in my book over and over and still nothing.

Seems to me I'm only comfortable with

cin >>
cout <<

Any tip/suggestion/help is highly welcome, thanks for using abit of your life-span to teach a newbie <3.
(The book I'm reading is a Swedish book 'C++ Direkt Jan Skansholm')
Last edited on
These are rather large topics to cover completely. Here is a basic overview. Let's start with arrays, since they are most simple.

Arrays

An array is just an index of numbers. It's really just a way of using large amounts of numbers conveniently. Let's take an example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 
#include <iostream>

int main()
{
    //This program just outputs square numbers up to .
    int x[50] = {0}; //Make an array of 50 numbers and set them all to 0.
    int i = 0; //Just an iterator to go through the while loop. Set to 0.
    for(i = 0;i < 50; ++i)
    {
        x[i] = i * i; //Make the i th element of x equal to the square of i.
        std::cout << i << " ^ 2 = " << x[i] << "\n"; //Output the number at location i in the array, x.
    }
    return 0;
}


This is a lot more convenient than writing out 50 separate variables to store these values. You can also now easily access these numbers because they are indexed. For example, to get the square of 33 you need only do:
 
std::cout << x[33];


So arrays are a very effective ways of storing and retrieving numbers.

Pointers

Pointers really can't be explained any better than this article (http://www.cplusplus.com/doc/tutorial/pointers/).

Classes

This is a more advanced topic. I really think you should be more comfortable with pointers and arrays and definitely functions and structs, before tackling classes. You don't mention how comfortable you are with those yet?
Last edited on
Thanks! @Mats

functions and structs should be on the list also.
I didn't want to overwhelm the reader with to much question to be answered, tho I thank you deeply, for pointing them out.

I'll play around and see what I can learn from your Array example and I'll re-read about pointers, strucs and functions, thanks again!




I tried to make an array that counted from 0 to 50 and after some thought and some code borrow from your array I managed to do this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;

int main()
{
	int x[50] = {0};
	int i = 0;
	for(i = 0;i <= 50; ++i)
	{
	x[i] = i + i;
	cout << i << " + " << x[i] << endl;
	}
	cin.get();
	return 0;
}


I tried playing around alot more with you code, but however I failed sadly at every attempt, the only thing I managed was to add some more text to it:

"Square root of 1 is 1" :(

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

int main()
{
    //This program just outputs square numbers up to .
    int long x[50] = {0}; //Make an array of 50 numbers and set them all to 0.
    int long i = 0; //Just an iterator to go through the while loop. Set to 0.
    for(i = 0;i < 50; ++i)
    {
        x[i] = i * i; //Make the i th element of x equal to the square of i.
        std::cout << "Square root of " << i[x] << " is " <<  i << "\n"; //Output the number at location i in the array, x.
    }
	std::cin.get();
    return 0;
}
Last edited on
x[i] = i + i

This means that the array element at i will be equal to i + i, or double i. To get x[i] equal to just i, you just need to put:
x[i] = i;

A few suggestions for you to try:

-Make an array of 10 elements and make all elements equal to 10 and then print them.
-Make an array of 10 elements containing only multiples of 3.
-Fill an array of 10 elements using a loop and user input from cin >> and print the array.
-Make an array of 10 elements using user input from cin and then sort it lowest - highest and print it.

Then you should be decent at using arrays.
Thanks I'll get right on it!

Is this correct or am I totaly wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// Make an array of 10 elements and make all elements equal to 10 and then print them. 
#include <iostream>

int main () 
{

	int x[10] = {0};

	int i = 0;
	for (i = 0; i < 10; ++i)
	{
		x[i] = 10;
		std::cout << x[i] << std::endl;

	}
		std::cin.get();
		return 0;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// Make an array of 10 elements containing only multiples of 3. . 
#include <iostream>

int main () 
{

	int x[10] = {0};

	int i = 0;
	for (i = 0; i < 10; ++i)
	{
		x[i] = i * 3;
		std::cout << i  << " multiple by 3 is " << x[i] << std::endl;

	}
		std::cin.get();
		return 0;
}
Last edited on
Topic archived. No new replies allowed.