• Amused
  • Angry
  • Bored
  • Busy
  • Cheeky
  • Cold
  • Confused
  • Cool
  • Devilish
  • Fine
  • Hacker
  • Happy
  • In Love
  • Innocent
  • Lonely
  • Nerdy
  • Pensive
  • Roflol
  • Sad
  • Starving
  • Stressed
  • Yeehaw
  • Page 3 of 79 FirstFirst 123451353 ... LastLast
    Results 21 to 30 of 782

    Thread: The Official Programming Thread

    1. #21
      PG Juggernaut

      I am:
      Pensive
       
      PSN ID: megamanexe95 Steam ID: www.steamcommunity.com/id/MegamanEXE
      MainMy Activity InfoMy PG Info
      Posts
      1,961
      Daily Activity
      0%
      Lv. Percent
      39.02%
      Quote Originally Posted by Gizmo View Post
      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
      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 by MegamanEXE; 22-08-14 at 07:56 PM. Reason: Messed up formatting -_-

    2. #22
      PG Juggernaut
      Team PG Emeritus

      I am:
      Starving
       
      Gamertag: Gizmo the Gamer
      MainMy Activity InfoMy PG Info
      Posts
      12,884
      Daily Activity
      0%
      Lv. Percent
      0.92%
      ^^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 by Gizmo; 22-08-14 at 08:15 PM.


    3. #23
      PG Newbie

      I am:
      Fine
       
      MainMy Activity InfoMy PG Info
      Posts
      5
      Daily Activity
      0%
      Lv. Percent
      76.25%
      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];
      }
      Likes Gizmo liked this post
       

    4. #24
      PG Juggernaut

      I am:
      Pensive
       
      PSN ID: megamanexe95 Steam ID: www.steamcommunity.com/id/MegamanEXE
      MainMy Activity InfoMy PG Info
      Posts
      1,961
      Daily Activity
      0%
      Lv. Percent
      39.02%
      Quote Originally Posted by Gizmo View Post
      ^^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.
      Likes Kashan123999 liked this post
       

    5. #25
      PG Juggernaut
      Team PG Emeritus

      I am:
      Starving
       
      Gamertag: Gizmo the Gamer
      MainMy Activity InfoMy PG Info
      Posts
      12,884
      Daily Activity
      0%
      Lv. Percent
      0.92%
      @mmmova ........ Yeah, you're right, What a stupid mistake 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 by Gizmo; 22-08-14 at 10:10 PM.


    6. #26
      PG Juggernaut

      I am:
      Pensive
       
      PSN ID: megamanexe95 Steam ID: www.steamcommunity.com/id/MegamanEXE
      MainMy Activity InfoMy PG Info
      Posts
      1,961
      Daily Activity
      0%
      Lv. Percent
      39.02%
      Quote Originally Posted by Gizmo View Post
      @mmmova ........ Yeah, you're right, What a stupid mistake 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.

    7. #27
      PG Juggernaut
      Team PG Emeritus

      I am:
      Starving
       
      Gamertag: Gizmo the Gamer
      MainMy Activity InfoMy PG Info
      Posts
      12,884
      Daily Activity
      0%
      Lv. Percent
      0.92%
      ^^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


    8. #28
      Lazy guy :s
        EternalBlizzard's Avatar

      I am:
      Bored
       
      MainMy Activity InfoMy PG Info
      Posts
      2,549
      Daily Activity
      0%
      Lv. Percent
      96.13%
      ^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...

    9. #29
      PG Juggernaut

      I am:
      Pensive
       
      PSN ID: megamanexe95 Steam ID: www.steamcommunity.com/id/MegamanEXE
      MainMy Activity InfoMy PG Info
      Posts
      1,961
      Daily Activity
      0%
      Lv. Percent
      39.02%
      Quote Originally Posted by Gizmo View Post
      ^^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 )

      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 by MegamanEXE; 23-08-14 at 01:22 AM.
      Likes Kashan123999 liked this post
       

    10. #30
      PG Juggernaut
      Team PG Emeritus

      I am:
      Starving
       
      Gamertag: Gizmo the Gamer
      MainMy Activity InfoMy PG Info
      Posts
      12,884
      Daily Activity
      0%
      Lv. Percent
      0.92%
      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
      @MegamanEXE ..... 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


     

     

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •  
    All times are GMT +5. The time now is 05:58 AM.


    Back to Top Search Engine Optimisation provided by DragonByte SEO (Pro) - vBulletin Mods & Addons Copyright © 2019 DragonByte Technologies Ltd.
    Shoutbox provided by vBShout (Pro) - vBulletin Mods & Addons Copyright © 2019 DragonByte Technologies Ltd.
    Copyright 2018 PakGamers. All rights reserved.