The Official Programming Thread

MegamanEXE

Well-known member
Dec 7, 2007
1,958
1
43
28
Islamabad
I have a C++ problem. I'm learning about pointers atm and I did this:


Code:
int Table(int a, int b){

int **p_p_table;

 p_p_table = new int*[a];

 for(int i=0; i<a; i++){

    p_p_table[a] = new int[b];


 }

 for(int i=0; i<a; i++){

    for(int j=0; j<b; j++){


        p_p_table[i][j] = i*j;
    }
    }

    for(int i=0; i<1; i++){

        for(int j=0; j<b; j++){

            cout << p_p_table[i][j] << "  ";
        }
        cout << " " << endl;
    }




}

int main ()
{
 int height;
 int width;

 cout << "Enter the Dimensions of the table: ";
 cin >> height;
 cin >> width;

  Table(height,width);


}

Basically, It's suppossed to be a function that let's the user create a multiplication table of their desired dimensions but whenever I run it and entert he dimension the program crashes because I'm not allocatting the memory correctly, How exactly do I do that?
@StrikerX ........ Batao mujhe :crazy:
Sup dude.
Well I'm sure entirely sure what you're trying to do, but first of all, I don't think you need a 2D array for a multiplication table, a 1D array is suitable. Secondly, since your function is only printing and not returning anything, so it should be void Table(int a, int b).

Also, it's a good idea to control your coding style now, don't leave meaningless spaces if you can help it, better to make this a habit early, secondly give the variables a better name especially for functions. Here's a modified version of your code with one-dimensional array if you want.
Code:
#include<iostream>
using namespace std;


void Table(int a, int b)
{
    int *p_p_table = new int[b+1]; //To go uptil 10
        
        for(int j=0; j<(b+1); j++){
            p_p_table[j] = a*j;
        }
    
        for(int j=1; j<(b+1); j++){
            cout <<a <<" x " <<j <<" = " <<p_p_table[j] <<endl;
        }
    }


int main ()
{
    int height;
    int width;


    cout << "Enter the Dimensions of the table: ";
    cin >> height;
    cin >> width;


    Table(height,width);
}
 
Last edited:

Gizmo

Expert
May 6, 2009
12,863
2
42
Lahore
^^The point is to make a two deimensional multiplication table using pointers so that the user can decide for themselves what the dimensions of the table should be when the program runs. So I'm trying to make 2 D array using pointers to achieve that.

I made a pointer to pointer thingy then used that to create an array of pointers in which each pointer points to another array. That's how it was done in the boook i'm learning from anyways but it's messed up.

I'm trying to get a result like this:

1 2 3 4 5 6
2 4 6 8 10 12

and the user should decide what the dimensions are when the program runs. The point of the exercise basically, is to DYNAMICALLY allocate a 2-D array.
 
Last edited:

mmmova

New member
Dec 12, 2007
5
0
1
Karachi
Hi,
The reasons for access violation is the index in the first loop inside Table function.It is only allocating space to the last row. Change
Code:
for(int i=0; i<a; i++){    
   p_p_table[a] = new int[b];
}
to
Code:
for(int i=0; i<a; i++){    
   p_p_table[i] = new int[b];
}
 

MegamanEXE

Well-known member
Dec 7, 2007
1,958
1
43
28
Islamabad
^^The point is to make a two deimensional multiplication table using pointers so that the user can decide for themselves what the dimensions of the table should be when the program runs. So I'm trying to make 2 D array using pointers to achieve that.

I made a pointer to pointer thingy then used that to create an array of pointers in which each pointer points to another array. That's how it was done in the boook i'm learning from anyways but it's messed up.

I'm trying to get a result like this:

1 2 3 4 5 6
2 4 6 8 10 12

and the user should decide what the dimensions are when the program runs. The point of the exercise basically, is to DYNAMICALLY allocate a 2-D array.
Oh, a classic. Here you go :)
Code:
#include<iostream>
using namespace std;


void Table(int a, int b)
{
	int** someTable = new int*[a];
	for(int i=0; i<a; i++)
		someTable[i] = new int[b];


	for(int i=1; i<a; i++){
		for(int j=1; j<b; j++){
			someTable[i][j] = i*j;
			cout <<"\t" <<someTable[i][j];
		}
		cout<<endl;
	}


	//DELETING STUFF; Just go in reverse :P
	for(int i=0; i<a; i++)
		delete someTable[i];
	delete[] someTable;
}


int main ()
{
	int height;
	int width;


	cout << "Enter the Dimensions of the table: ";
	cin >> height >>width;


	Table(height,width);
}
You should really read "Object-Oriented Programming in C++ by Robert Lafore", I found it to be the best for beginners. I tried reading others, they go fine at first, then the author just gets tired of 'holding hands' and go into details without caring. Lafore's book is my favorite. :)
 

Gizmo

Expert
May 6, 2009
12,863
2
42
Lahore
@mmmova ........ Yeah, you're right, What a stupid mistake :fp2: Welp atleast it shows that my understanding of the subject isn't fundamentally flawed so far, Need to be more careful while typing these out.

Thanks. :)
@MegamanEXE ...... I started learning with that book you mentioned but it got kind of bored of it after a while, Now I'm learning from "Jumping into C++ by Alex Allain" and I prefer it's style over the former's. ANd it's been OK so far althought it's not as detailed as Lafor'es book.
 
Last edited:

MegamanEXE

Well-known member
Dec 7, 2007
1,958
1
43
28
Islamabad
@mmmova ........ Yeah, you're right, What a stupid mistake :fp2: Welp atleast it shows that my understanding of the subject isn't fundamentally flawed so far, Need to be more careful while typing these out.

Thanks. :)
@MegamanEXE ...... I started learning with that book you mentioned but it got kind of bored of it after a while, Now I'm learning from "Jumping into C++ by Alex Allain" and I prefer it's style over the former's. ANd it's been OK so far althought it's not as detailed as Lafore's book.
Oh, well I agree the first chapter was a bit intimidating but you can completely ignore that one. I tried some other books before choosing this one, it's consistent and goes up gradually into topics. The other books are a bit jumpy, they explain some stuff thinking you might be 5 years old, then suddenly explain something like you turned 20 years old in 5 minutes.
Lafore's book isn't THAT detailed, it's sufficiently detailed and gives you a pretty good idea of the concept which you can then easily carry over to other languages.

As for problems like these, as a starting point, ignore the fact that you have to make a dynamically allocated array, just think of how you can make a 5x5 table, if you can do that, you just have to change the declaration of the array (you won't even have to change the name) from int Table[5][5] to however you usually allocate. :)
 

Gizmo

Expert
May 6, 2009
12,863
2
42
Lahore
^^Do they use RObert Lafore's book as the text book at NUST or did you just pick it up on your own?

FAST Lahore as the following books on the course:

"C++, How to Program?" by Deitel and Deitel

"C++ programming" by D.S Malik
 

EternalBlizzard

Lazy guy :s
Moderator
Oct 29, 2011
2,732
1,195
129
Attractor Field Beta
^Have you learned C ??
According to @ammarzubair

"when someone teaches c++ they assume they already know c and don't really teach all the basics that are needed. maybe that is the reason you got soo confused. "

Personally i think that's a good point...
 

MegamanEXE

Well-known member
Dec 7, 2007
1,958
1
43
28
Islamabad
^^Do they use RObert Lafore's book as the text book at NUST or did you just pick it up on your own?

FAST Lahore as the following books on the course:

"C++, How to Program?" by Deitel and Deitel

"C++ programming" by D.S Malik
lol no, I learnt it myself during free time in 10th class for the lulz, and studied absolutely nothing for board paper in I.C.S (Got 98/100 :3).

This helped me in the long run too. My first year at NUST is over and since I studied this myself. Got an A and top position in programming in our 2 sections of 100 people :3 It isn't a textbook, but I was surprised teachers recommend it as a side reference book (although nobody reads 'side' books :p)

When beginning, I wouldn't recommend Deitel & Deitel at all. The D&D book was the first programming book I ever tried to read (it was my brother's). The text is so conjusted and TOO much text and what not, and referencing things you learn much later on. I'd recommend you read it AFTER you've done sufficient programming since it has tips and stuff you'd really understand later on. If you showed the beginning code to someone who has never seen a programming language before, he'd go "nope, nope, nope, never learning programming ever".

Now that I have a good understanding of C++, my other brother in FAST also got the D&D book and I try to read it time and again. I'd definitely not recommend it to a beginner. It's not aimed at them, it's more of a reference.

As such, I'm not deadly recommending that one should always begin with Lafore's book. There may be better ones I haven't read that, so this is just a personal opinion.

@Eternal Blizzard
Well, not really, you can as easily jump into C++ as you can into C.

Also, somebody please fix the thread title. "The Official The Programming Thread" is really annoying. o_O
 
Last edited:

Gizmo

Expert
May 6, 2009
12,863
2
42
Lahore
You don't need to learn C first before you learn C++ atleast no according to robert Lafore and I've been doing OK with Alex Allain's book even though I hadn't even touched any language other than BASIC in the 9th grade lol
[MENTION=1394]MegamanEXE[/MENTION] ..... that's why I like Alex Allain's book so far, it's not very long, get's to the point but still conveys everything sufficiently well, atleast I think so :p
 

gow3

Well-known member
Jan 10, 2010
1,053
0
41
Is anyone else doing Jquery/JS/Web Development? What do you guys work with? I am currently working with the bootstrap framework.
 

MegamanEXE

Well-known member
Dec 7, 2007
1,958
1
43
28
Islamabad
Well i myself started C# first.... but i still think i should learn C first...
Well, C# doesn't actually have anything to do with C/C++. Anyway, you can directly go into C++. All C code is valid in C++, but all C++ code is not C code.

Really, when starting out, there is literally no difference between C and C++. You can use printf() and scanf() in C++ if you want. Even when learning C, people make .cpp files instead of .c files. Because EVEN PURE C CODE WILL RUN FLAWLESSLY.

Why .cpp instead of .c? Well, old C had a pretty terrible rule. Since you know C#, you'll get it. In C, you can only declare variable on the top of a fuction (including main()). Meaning you can't do
Code:
for(int i=0; i<10; i++)
in C. You can't do on-spot declarations. So you'll have to move int i to the top of main() and write
Code:
for(i=0; i<10; i++)
Annoying isn't it? When you need a variable, you need to go to the top to declare it. The code is exactly the same when you're beginning. (well, there's malloc() instead of new and strings :3).

So learning C++, you're pretty much learning C as well. Same is true in reverse. There's no loss in learning C first or C++. Your call really.
 

EternalBlizzard

Lazy guy :s
Moderator
Oct 29, 2011
2,732
1,195
129
Attractor Field Beta
Well, C# doesn't actually have anything to do with C/C++. Anyway, you can directly go into C++. All C code is valid in C++, but all C++ code is not C code.

Really, when starting out, there is literally no difference between C and C++. You can use printf() and scanf() in C++ if you want. Even when learning C, people make .cpp files instead of .c files. Because EVEN PURE C CODE WILL RUN FLAWLESSLY.

Why .cpp instead of .c? Well, old C had a pretty terrible rule. Since you know C#, you'll get it. In C, you can only declare variable on the top of a fuction (including main()). Meaning you can't do
Code:
for(int i=0; i<10; i++)
in C. You can't do on-spot declarations. So you'll have to move int i to the top of main() and write
Code:
for(i=0; i<10; i++)
Annoying isn't it? When you need a variable, you need to go to the top to declare it. The code is exactly the same when you're beginning. (well, there's malloc() instead of new and strings :3).

So learning C++, you're pretty much learning C as well. Same is true in reverse. There's no loss in learning C first or C++. Your call really.
Lol that's one annoying problem... awrite i get it...
Anyways i wanted to ask another thing Im learning Ruby from Codeacademy and im about to finish up, im on OOP part 1 so i was thinking how would i know if i have learnt this language completely and by completely i mean i should know every method, classes etc... or is it that you don't ever learn a language completely you just start working, making programs and gradually as u tackle problems you start to learn new things and after a few years of experience you master that language?
 

MegamanEXE

Well-known member
Dec 7, 2007
1,958
1
43
28
Islamabad
Lol that's one annoying problem... awrite i get it...
Anyways i wanted to ask another thing Im learning Ruby from Codeacademy and im about to finish up, im on OOP part 1 so i was thinking how would i know if i have learnt this language completely and by completely i mean i should know every method, classes etc... or is it that you don't ever learn a language completely you just start working, making programs and gradually as u tackle problems you start to learn new things and after a few years of experience you master that language?
As anyone on StackOverflow or anyone who has programmed for a long time will tell you. You can NOT learn a language completely. There are always new standards coming, new additions. You usually learn the basics and specialize in a branch or something. Learn what you need, don't expect to 'ratta' the Standard library, you probably can't do that anyway.
In short, learn the basics nicely, then learn what you need, not just 'learn' for the sake of learning. Otherwise you'll probably spend your lifetime learning something you'll probably never use. There are 3rd party libraries and what not. Once you learn one HLL well enough, you can very easily jump into other languages.

Although, my opinion differs from some people on the internet but I think you'll agree with me. Most people wouldn't recommend C/C++/Java (definitely not Java o_O) as a first language because it's 'difficult' (Although I still don't know in what sense, I felt it was quite natural) and learn Python, JavaScript etc.

But picture it like this. If you can finish a game on 'Hard' difficulty, you'll feel that the 'Normal' and 'Easy' difficulty is child's play and effortless. Same is the case with C/C++. The control they provide is hard to beat and they have less restrictions as well. So, the 'other' languages like C# and what not, they restrict the freedom which C/C++ provides and minimize control in some sense for clarity or to reduce mistakes. So, most stuff that's in C# is in C/C++ as well and it allows you to change it, for the better or for the worse. When used correcltly, you can make the program 10x better or 10x worse, BUT THE POWER IS STILL THERE! It's never the language's fault. Whether you need a C/C++ feature or not, it's there, whether you like it or not.

So, if you learn C/C++, you'll see how DRASTICALLY EASY it is to jump into C# with hardly any effort. You'll think something like "Oh, I could do that manually in C++, but C# automates that for me". But what if you want to customize that 'automation' process? C# won't let you. C/C++ will. Of course, just an example, nobody would want to mess with that though :p But just letting you know it IS possible.

OK, I'm stopping now.
/RANT
 

gullfounder

Phoenix Dawn
Supervisor
Aug 25, 2008
970
3
24
Khanpur
As anyone on StackOverflow or anyone who has programmed for a long time will tell you. You can NOT learn a language completely. There are always new standards coming, new additions. You usually learn the basics and specialize in a branch or something. Learn what you need, don't expect to 'ratta' the Standard library, you probably can't do that anyway.
In short, learn the basics nicely, then learn what you need, not just 'learn' for the sake of learning. Otherwise you'll probably spend your lifetime learning something you'll probably never use. There are 3rd party libraries and what not. Once you learn one HLL well enough, you can very easily jump into other languages.

Although, my opinion differs from some people on the internet but I think you'll agree with me. Most people wouldn't recommend C/C++/Java (definitely not Java o_O) as a first language because it's 'difficult' (Although I still don't know in what sense, I felt it was quite natural) and learn Python, JavaScript etc.

But picture it like this. If you can finish a game on 'Hard' difficulty, you'll feel that the 'Normal' and 'Easy' difficulty is child's play and effortless. Same is the case with C/C++. The control they provide is hard to beat and they have less restrictions as well. So, the 'other' languages like C# and what not, they restrict the freedom which C/C++ provides and minimize control in some sense for clarity or to reduce mistakes. So, most stuff that's in C# is in C/C++ as well and it allows you to change it, for the better or for the worse. When used correcltly, you can make the program 10x better or 10x worse, BUT THE POWER IS STILL THERE! It's never the language's fault. Whether you need a C/C++ feature or not, it's there, whether you like it or not.

So, if you learn C/C++, you'll see how DRASTICALLY EASY it is to jump into C# with hardly any effort. You'll think something like "Oh, I could do that manually in C++, but C# automates that for me". But what if you want to customize that 'automation' process? C# won't let you. C/C++ will. Of course, just an example, nobody would want to mess with that though :p But just letting you know it IS possible.

OK, I'm stopping now.
/RANT
You put it very nicely that there is no need to add much into it. Anyway, i am here to ask you some suggestion. I am programmer my self. Like C/C++ Loved C#. Did most my projects on C#. i love it how it handles the Hardware's. But here is my question. I want to get into Game Programming. i mostly did Database stuff Quite boring. But i need to change it now. Can you suggest me anything regarding this? Pleas Leave Unity out of this.
 

Gizmo

Expert
May 6, 2009
12,863
2
42
Lahore
^^Isn't C++ suppossed to be great for game development cus of the OOP stuff? ANyways I think it's a good idea to just learn Unity, When you learn the basics then you can use your programming skills to mod it to your liking later on, it's alot easier than trhying to make a game from Scratch in C++ or some other language where you'd have to code everything by yourself.
 

EternalBlizzard

Lazy guy :s
Moderator
Oct 29, 2011
2,732
1,195
129
Attractor Field Beta
@gullfounder
Unity uses C# and Lua/Boo (can't remember ) so if u want u can make scripts or get experience by working in unity (Me and my friend worked on it and it was a good experience )
secondly if u wanna learn a new language... C++ is there Cryengine uses it.... Thirdly, if u want u can come towards Ruby, RPGMakerVXA uses it and you can try building a nice game
@MegamanEXE
Hmm thanx for the info so after learning from codeacademy can i start making scripts? to use in Rpgmaker vxa engine? :D
I get what you are trying to say i'd like to quote from Rob Miles C# book if you allow....

Spoiler: show
I referred to C as a dangerous language. So what do I mean by that? Consider the chain saw. If I, Rob Miles, want to use a chain saw I will hire one from a shop. As I am not an experienced chain saw user I would expect it to come with lots of built in safety features such as guards and automatic cut outs. These will make me much safer with the thing but will probably limit the usefulness of the tool, i.e. because of all the safety stuff I might not be able to cut down certain kinds of tree. If I was a real lumberjack I would go out and buy a professional chain saw which has no safety features whatsoever but can be used to cut down most anything. If I make a mistake with the professional tool I could quite easily lose my leg, something the amateur machine would not let happen.In programming terms what this means is that C lacks some safety features provided by other programming languages. This makes the language much more flexible.
However, if I do something stupid C will not stop me, so I have a much greater chance of crashing the computer with a C program than I do with a safer language.

I reckon that you should always work on the basis that any computer will tolerate no errors on your part and anything that you do which is stupid will always cause a disaster! This concentrates the mind wonderfully.


I liked how he introduced it :D
I'll agree with you but for the meantime im still learning Ruby and i'll say its High Level and much easier to understand and use....
 
Last edited:
General chit-chat
Help Users
We have disabled traderscore and are working on a fix. There was a bug with the plugin | Click for Discord
  • No one is chatting at the moment.
    faraany3k faraany3k: Tears of Kingdom saal pehle shuru ki thee, ab tk pehle area se nai nikla. Life sucks donkey balls.