• Hey, guest user. Hope you're enjoying NeoGAF! Have you considered registering for an account? Come join us and add your take to the daily discourse.

Programming |OT| C is better than C++! No, C++ is better than C

In my computer security class we have projects to build DES and AES without the use of extra libraries especially ones that do this for you.

I'm just about done debugging it. I've identified a problem in my mix columns and also probably in my inverse one. I've learned so much doing this it has been quite the experience.

I was just ranting yesterday when I couldn't limit it down to where it was going wrong. Every other function appears to be working correctly when I force feed it a state that I know the answer to.
ok, fair enough.

Curious: Do they let you use table lookups, or do you have to do it all with constant-time operations?

E: I had 'constant-time instructions', which is not what I meant...
 

NotBacon

Member
That book is reallllllly advanced. I strongly don't recommend it unless you already have a decent grasp on C++11.

I don't know what the best intro to C++11 is right now. Probably this: http://www.amazon.com/gp/product/0321563840/?tag=neogaf0e-20

Edit: Also this for the standard library: http://www.amazon.com/gp/product/0321623215/?tag=neogaf0e-20

Fun fact about rand(). RAND_MAX is only required by the standard to be at least 32767. And on MSVC, that's exactly what it is. Let that sink in.

Yeah TC++PL is more comprehensive than I was hoping for, but it's looking like the best option right now.
1360 pages!

As for rand()...... O_O

Android development question:

I am creating an app which monitors screen touches over a certain period of time and displays them as a heatmap in realtime. I would like to save these touches in a file so I can see the touch/heatmap again if I need to.

I am new to this, so far I have the basic app structure with a Navigation Drawer and an item selected listener. Each option opens a fragment instead of a new activity, I am still not 100% sure if this is the correct decision, I need to do further reading to compare the two.

My main concerns are am I right to use fragments and what is the best way to detect and show these screen touches? For the screen touches I have been doing some research and currently looking to use an onTouchEvent listener and use a draw function to plot these points on the screen. Am I on the right track here? Is there any advice or anything I should be aware of moving forward for this project?

Thank you :)

Yeah that sounds right. You definitely was to use fragments as lightweight "sections" in the nav. drawer. Here is a great guide for a modern nav. drawer. Really anything from Codepath is pretty good.

As for the data, OminoMichelin is right. Stuff all the data you need into a database, then just query and draw into a bitmap buffer when you want to display.
 

Fishlake

Member
ok, fair enough.

Curious: Do they let you use table lookups, or do you have to do it all with constant-time operations?

E: I had 'constant-time instructions', which is not what I meant...

Yes they let us use tables for the mixColumns and subBytes steps. It would probably be extra credit if you did it that way but my professor says it gets quite nasty so we would not be doing it in his class.

I've got it working 100% now so that feels pretty good. These two programs have definitely been the most impressive programs I've written for a class. I've never programmed on a binary level before this.
 

Slavik81

Member
I asked this in the linux distro thread but someone here might be able to help as well

so I have a question about running csh scripts from bash. I defined the environmental variables the csh script uses in my bashrc file by using export instead of setenv. And then I executed the script from bash. It seems to work fine.

I was wondering if there is another efficient way to do this instead.

examples would be great.
You can define variables for the command with VARIABLE=value right before the command.
Code:
$ MY_VAR='this is my variable!' tcsh -c 'echo $MY_VAR'
this is my variable!
 
I'm having a bit of a mental block today. I'm looking at coding problems online and can't seem to figure out a good way to do this:

Lets say I have an array [1, 7 , 7 , 9 , 10, 7, 5, 5, 5].

I want to find the modes (7 and 5 in this example), and then add those values to another array or a vector. However, the values shouldn't be added more than once.

Thus the second array or vector should be: [7, 5]
not [7, 7, 7, 5, 5, 5]

Finding the mode is simple, but I can't figure out a good way to prevent adding the mode more than once. Other than looping through the entire vector/second array and checking if the value is already in there. But that's a really inefficient solution.
 

Fishlake

Member
Could you use a array that counts how often each appear then just send over those with the max count. You could have a int that is max and then when you have gong through the entire thing compare the amount that showed up with the max and if it is equal to the max transfer over what is at that spot.

If you are using non numbers have a second array that holds onto those.

Does this sound like it will solve your problem?
 
I'm having a bit of a mental block today. I'm looking at coding problems online and can't seem to figure out a good way to do this:

Lets say I have an array [1, 7 , 7 , 9 , 10, 7, 5, 5, 5].

I want to find the modes (7 and 5 in this example), and then add those values to another array or a vector. However, the values shouldn't be added more than once.

Thus the second array or vector should be: [7, 5]
not [7, 7, 7, 5, 5, 5]

Finding the mode is simple, but I can't figure out a good way to prevent adding the mode more than once. Other than looping through the entire vector/second array and checking if the value is already in there. But that's a really inefficient solution.

Why would it be added more than once? The mode of a sequence is by definition a set of unique numbers. After your algorithm computes the number 7 for the mode, how could it ever possibly compute 7 again? So for each mode you compute, just add it to the array, there will never be a duplicate
 
Why would it be added more than once? The mode of a sequence is by definition a set of unique numbers. After your algorithm computes the number 7 for the mode, how could it ever possibly compute 7 again? So for each mode you compute, just add it to the array, there will never be a duplicate

I'm going to PM you what I have written because that isn't how my code works.

Could you use a array that counts how often each appear then just send over those with the max count. You could have a int that is max and then when you have gong through the entire thing compare the amount that showed up with the max and if it is equal to the max transfer over what is at that spot.

If you are using non numbers have a second array that holds onto those.

Does this sound like it will solve your problem?

I'm not really seeing what you mean here.

So in the example I gave we have the array [1, 7 , 7 , 9 , 10, 7, 5, 5, 5]. The max count if I understand you correctly is 3.

You are suggesting to make an array with [1, 1, 2, 1, 1, 3, 1, 2, 3]?
 
Thus the second array or vector should be: [7, 5]
not [7, 7, 7, 5, 5, 5]

Finding the mode is simple, but I can't figure out a good way to prevent adding the mode more than once. Other than looping through the entire vector/second array and checking if the value is already in there. But that's a really inefficient solution.

Create a Set and then convert to required type?
 

Fishlake

Member
I'm not really seeing what you mean here.

So in the example I gave we have the array [1, 7 , 7 , 9 , 10, 7, 5, 5, 5]. The max count if I understand you correctly is 3.

You are suggesting to make an array with [1, 1, 2, 1, 1, 3, 1, 2, 3]?

Yes I have t count how often things appear in my AES and doing it this way would allow you too them know immediately if you need to add it to the mode array. Its just like scanning a document for how often a character appears. So if you use the array as

for(i = 1; i <= 10; i++){
if(count[i-1] == max) save it (i) to the mode array.
}
 
Yes I have t count how often things appear in my AES and doing it this way would allow you too them know immediately if you need to add it to the mode array. Its just like scanning a document for how often a character appears. So if you use the array as

for(i = 1; i <= 10; i++){
if(count[i-1] == max) save it (i) to the mode array.
}

Whats the best way to get the second array though?

if I have array1 = {1, 7 , 7 , 9 , 10, 7, 5, 5, 5}

array2[9];

I need to get array2 to be {1,1,2,1,1,3,1,2,3].

I'm not quite sure I know how to accomplish that.
 

upandaway

Member
How much harder is C++ to learn than Java?
We just started learning it in an advanced programming class, and from a first impression I'd say a lot harder. I'd say Java already lets you do almost anything you want just by fixing things as you go, in C++ that's definitely definitely not the case. Every time I go into a problem without having mastered the basics I just fall immediately, and if I'm lucky enough to get a compiler error it's usually nothing better than "hey an error happened somewhere sometime"
 

Fishlake

Member
Whats the best way to get the second array though?

if I have array1 = {1, 7 , 7 , 9 , 10, 7, 5, 5, 5}

array2[9];

I need to get array2 to be {1,1,2,1,1,3,1,2,3].

I'm not quite sure I know how to accomplish that.

Just go through the array of data you have and at each one have count[data]++; Make count an array as big as the range of numbers and you will be fine. It is rather simple with numbers. If you need to apply it to characters use the ascii values.
 
ok so I think this would work?

Code:
#include <iostream>

using std::cout;
using std::endl;

int main()
{

int array1[9] = {1, 5, 5, 5, 7, 7, 7, 9, 10};

int array2[9];

int num = array1[0];
int frequency = 0;

for (int i = 0; i < 9; i++)
{
    if (array1[i] == num)
    {
        frequency++;
        array2[i] = frequency;

    }

    else
    {
        frequency = 1;
        array2[i] = frequency;
        num = array1[i];
    }


}

for(int i = 0; i < 9; i++)
{
    cout << array2[i] << endl;
}

return 0;
}
 
I would do something like this.

For (int i = 0; i < 9;i++){
array2[array1]++;
}
first set the count array to 0.


Perhaps I'm not understanding.

In the example I gave we have {1, 5, 5, 5, 7, 7, 7, 9, 10}

So in your for loop:

i =0
array2[array1[0]]++;
which is array2[1]++?
 

Fishlake

Member
Perhaps I'm not understanding.

In the example I gave we have {1, 5, 5, 5, 7, 7, 7, 9, 10}

So in your for loop:

i =0
array2[array1[0]]++;
which is array2[1]++?

Yup you increase the count of array2[1] at i = 0, array2[5] at i = 1 and so on through the data array. At the end of the loop you have how often 0-10 appear in the data.
 
Yup you increase the count of array2[1] at i = 0, array2[5] at i = 1 and so on through the data array. At the end of the loop you have how often 0-10 appear in the data.

Wouldn't this neglect to change the 0th value of array2? And then skip the 2nd, 3rd, and 4th elements?

or... I don't think I'm getting this. I don't see how you're code would create a parallel array.
 

Fishlake

Member
Wouldn't this neglect to change the 0th value of array2? And then skip the 2nd, 3rd, and 4th elements?

or... I don't think I'm getting this. I don't see how you're code would create a parallel array.

The idea is that each spot in array 2 corresponds to its place in the array. So array2[0] tells how often 0 appears in the data. 0 times.
 
The idea is that each spot in array 2 corresponds to its place in the array. So array2[0] tells how often 0 appears in the data. 0 times.

Oh I see. Yeah my idea was a bit different.

Though then in your case if array1 is just {100} array2 needs to be able to hold 100 elements?
 
Decided to start learning programming today and ran through a few beginner lessons. One of the extra assignments was to make a change sorting program (enter an amount, output the most efficient change back). My logic ends up being something this

Code:
    int change_output = 1 + change_input * 100;
    
    int quarters = change_output / 25;
    change_output = change_output % 25;
    int dimes = change_output / 10;
    change_output = change_output % 10;
    int nickels = change_output / 5;
    change_output = change_output % 5;
    int pennies = change_output;

But surely there is a more efficient way to do this that I'm not thinking of. It feels like something that can be looped, I just can't wrap my head around it right now. Feel free to tear apart any mistakes I've made or best practices I've broken. Harsh criticism cracks the whip or whatever the phrase is.
 
Decided to start learning programming today and ran through a few beginner lessons. One of the extra assignments was to make a change sorting program (enter an amount, output the most efficient change back). My logic ends up being something this

Code:
    int change_output = 1 + change_input * 100;
    
    int quarters = change_output / 25;
    change_output = change_output % 25;
    int dimes = change_output / 10;
    change_output = change_output % 10;
    int nickels = change_output / 5;
    change_output = change_output % 5;
    int pennies = change_output;

But surely there is a more efficient way to do this that I'm not thinking of. It feels like something that can be looped, I just can't wrap my head around it right now. Feel free to tear apart any mistakes I've made or best practices I've broken. Harsh criticism cracks the whip or whatever the phrase is.

Code:
const int num_denominations = 4;
int denominations[num_denominations] = {25, 10, 5, 1};
int quantities[num_denominations] = {0, 0, 0, 0};

for (int i=0; i < num_denominations; ++i)
{
    // See if you can fill out this part.
}

Edit: Also why are you adding 1? If you give it change_input == 0 (which should mean no money at all) it will tell you that you need 1 penny.

Edit 2: Also x = x % 5 can be written more succintly as x %= 5
 
Edit: Also why are you adding 1? If you give it change_input == 0 (which should mean no money at all) it will tell you that you need 1 penny.

Edit 2: Also x = x % 5 can be written more succintly as x %= 5

Ah! I should have added that the initial input is a float (Takes a number like 3.54) and converted into an int. So to account for the rounding down (when I ran "2.10" through I got a result for "2.09") I added "1". It hadn't even occurred to me about making sure that "0" still gave an accurate answer. Thanks for the correction.

Code:
int main(void) {
    // const int num_denominations = 4;
    int denominations[4] = {25,10,5,1};
    // memset(denominations, 4, num_denominations*sizeof(int));
    int quantities[4] = {0,0,0,0};
    // memset(quantities, 4, num_denominations*sizeof(int));
    
    printf("How much change do you need?  ");
    float change_input = GetFloat();
    int change = change_input * 100;
    
    for(int i=0; i < 5; i++) {
        if(change > 0) {
            quantities[i] = change / denominations[i];
            change %= denominations[i];
        }
    }
    printf("There are %i quarters, %i dimes, %i nickels, and %i pennies.\n"
, quantities[0], quantities[1],quantities[2],quantities[3]);
}

Ultimately ended up with this. Which seems much better. I had an issue using a variable as the size of the array even though it was a constant. Maybe my compiler? I commented out my attempts to fix the issue since they weren't working and then cheated by shoving 4 in as the size.
 
Unrelated but I really prefer python lists to C++ arrays. Though I greatly prefer C++ classes to python classes.

C++ arrays are extremely barebones and essentially are meant to just represent a type-safe chunk of memory. They can't have any features that would require having extra storage per array, transparently moving stuff around in memory (which would invalidate your pointers), or extra checks on every array access. That's why they're so tricky to use. If you want a data structure with the performance characteristics of an array but don't need it to be low level, it's nearly always better to use a vector. But even vectors don't check index bounds unless you use the at method.

The bottom line is that C++ (for the most part) is heavily optimized for performance over ease of use. Python's the opposite. I'd be interested if your opinion on classes remains the same after working with both languages for a while.
 

Makai

Member
Custom operators are amazing. Any feature I miss from C# I can trivially implement into F#. I don't think I'm going back.
 

Rush_Khan

Member
Hello World! I have two questions.

Firstly, is Python worth learning if you're already proficient in C++ (well, up to OOP stuff like Inheritance) and very familiar with Java? I'm trying to look for a new language which is in high demand with companies (software, games, etc.).

Secondly, what is the most predominant language used in engines for most modern games? C#? Javascript? Does it vary between game engines? The only game-making experience I have is with Game Maker (GML, that's my most fluent language) and a tiny bit of C# whilst playing around with Unity.
 

Makai

Member
Hello World! I have two questions.

Firstly, is Python worth learning if you're already proficient in C++ (well, up to OOP stuff like Inheritance) and very familiar with Java? I'm trying to look for a new language which is in high demand with companies (software, games, etc.).

Secondly, what is the most predominant language used in engines for most modern games? C#? Javascript? Does it vary between game engines? The only game-making experience I have is with Game Maker (GML, that's my most fluent language) and a tiny bit of C# whilst playing around with Unity.
Unreal Engine is written in C++ and even scripted in C++. Check it out. If you want to learn a new language, a better use of your time might be to try something entirely different to broaden your horizons as a programmer. Python is a good fit for that.
 

Rush_Khan

Member
Unreal Engine is written in C++ and even scripted in C++. Check it out. If you want to learn a new language, a better use of your time might be to try something entirely different to broaden your horizons as a programmer. Python is a good fit for that.

I'll take a look at Python as per your advice. I briefly glanced at the syntax and the indentation stuff looks pretty silly, but I guess it's standard convention to indent blocks of code in other languages so it should be okay.

Also, wow! I had no idea Unreal was free for everyone. That's awesome. Thanks.
 

Makai

Member
I'll take a look at Python as per your advice. I briefly glanced at the syntax and the indentation stuff looks pretty silly, but I guess it's standard convention to indent blocks of code in other languages so it should be okay.

Also, wow! I had no idea Unreal was free for everyone. That's awesome. Thanks.
My personal recommendation would be to check out Haskell. Not because it's commonly used by enterprise, but because it will change the way you think about programming.
 
Oh wow! I didn't expect a thread like this on GAF. I'll have to keep checking in as I try to find the motivation to code in my free time instead of just playing games and prepare for taking 4 comp Sci classes in the spring along with trying to get a local internship.
 
Ah! I should have added that the initial input is a float (Takes a number like 3.54) and converted into an int. So to account for the rounding down (when I ran "2.10" through I got a result for "2.09") I added "1". It hadn't even occurred to me about making sure that "0" still gave an accurate answer. Thanks for the correction.

Even if you get hit by rounding error with one number, you won't get hit by it with another number. So it's best to solve that using a different approach, because adding 1 makes an assumption that rounding error will always occur.
 
I've played around with React.js some over the last couple of days and I kinda like it. However, I'm still confused about Javascript modules and package management and bundling and all that. My current Javascript workflow is "put script tags into HTML" and be done with it.
However it gets more complicated with React because I want to use the JSX syntax and also a ES6 transpiler (Babel). I tried using a library (React-Router) that kinda didn't work when I just dropped it in as a script tag and every code example I found was using something like the following:
Code:
import { Router, Route, Link } from 'react-router'
There's a lot of stuff and it all seems to do slightly different things. Like npm, which is the Node package manager, but apparently also has client-side Javascript libraries, then there's Bower, which is also a package manager, but different somehow (?), then I looked at Webpack, which seems to be used to bundle resources. I feel lost. Is there an article that just gives me a good overview over what tool does what and ideally gives me some analogues for other languages?
 

Karl2177

Member
I'm having some trouble using Gson in Java. I keep getting "Expected BEGIN_OBJECT but was BEGIN_ARRAY" and the stack overflow pages haven't been helpful. I'll post the code when I get to the computer that has it. Just thought I'd ask if anyone has experience with Gson first.
 

NotBacon

Member
I'm having some trouble using Gson in Java. I keep getting "Expected BEGIN_OBJECT but was BEGIN_ARRAY" and the stack overflow pages haven't been helpful. I'll post the code when I get to the computer that has it. Just thought I'd ask if anyone has experience with Gson first.

Sounds like the JSON you're de-serializing doesn't match your Java object model
 

Karl2177

Member
I was mistaken also. The actual error message was "BEGIN_ARRAY but was BEGIN_OBJECT".
Sounds like your object was wrapped in [] instead of {}

Sounds like the JSON you're de-serializing doesn't match your Java object model

That's it. My problem now is with json syntax.

Here's the json code in particular:
Code:
"ResultCount": "int",
  "Results": [
    {
      "Links": "links",
      "Id": {
        "MatchId": "guid",
        "GameMode": "int",
      },...
     }
    ]

So when I create the object, is the only thing in the Results class a List? If that is the case, what is it a list of? To me, the way I'm reading it is that it is a list of "Results", but that seems to go against the syntax.
 

JeTmAn81

Member
That's it. My problem now is with json syntax.

Here's the json code in particular:
Code:
"ResultCount": "int",
  "Results": [
    {
      "Links": "links",
      "Id": {
        "MatchId": "guid",
        "GameMode": "int",
      },...
     }
    ]

So when I create the object, is the only thing in the Results class a List? If that is the case, what is it a list of? To me, the way I'm reading it is that it is a list of "Results", but that seems to go against the syntax.

If ResultCount is supposed to be part of your JSON object, you'll want to have { before it. { marks the beginning of an object, } marks the end, and the same goes for [] with arrays. You'll also want to add another } to close the overall object. It also looks like you've got Results in an array of size one with a Javascript object in it. Is there any reason that needs to be an array?
 

Karl2177

Member
If ResultCount is supposed to be part of your JSON object, you'll want to have { before it. { marks the beginning of an object, } marks the end, and the same goes for [] with arrays. You'll also want to add another } to close the overall object. It also looks like you've got Results in an array of size one with a Javascript object in it. Is there any reason that needs to be an array?

I cut a bunch of stuff out. The array itself will hold anywhere from 1-25 of those objects(?) in it. Here's the actual json, removed a few lines at the end and changed some values that are meant to be internal use only.
Code:
{  
   "Start":0,
   "Count":25,
   "ResultCount":25,
"Results":[  
      {  
         "Links":{  },
         "Id":{  },
         "HopperId":"hexcode",
         "MapId":"hexcode",
         "MapVariant":{  },
         "GameBaseVariantId":"hex",
         "GameVariant":{  },
         "MatchDuration":"PT8M46.2464205S",
         "MatchCompletedDate":{  },
         "Teams":[  ],
         "Players":[  
            {  }
         ],
         "IsTeamGame":true,
         "SeasonId":"hex"
      },
      {  
         "Links":{  
            "StatsMatchDetails":{  
               "AuthorityId":"spartanstats",
               "Path":"urlpath",
               "QueryString":null,
               "RetryPolicyId":"exponentialretry",
               "TopicName":"",
               "AcknowledgementTypeId":0,
               "AuthenticationLifetimeExtensionSupported":false
            },
            "UgcFilmManifest":{  
               "AuthorityId":"ugc",
               "Path":"urlpath",
               "QueryString":"?view=film-manifest",
               "RetryPolicyId":"exponentialretry",
               "TopicName":"",
               "AcknowledgementTypeId":0,
               "AuthenticationLifetimeExtensionSupported":false
            }
         },
         "Id":{  
            "MatchId":"hex",
            "GameMode":1
         },
         "HopperId":"hex",
         "MapId":"hex",
         "MapVariant":{  
            "ResourceType":3,
            "ResourceId":"hex",
            "OwnerType":3,
            "Owner":""
         },
         "GameBaseVariantId":"hex",
         "GameVariant":{  
            "ResourceType":2,
            "ResourceId":"hex",
            "OwnerType":3,
            "Owner":""
         },
         "MatchDuration":"PT8M20.5707417S",
         "MatchCompletedDate":{  
            "ISO8601Date":"2015-11-08T00:00:00Z"
         },
         "Teams":[  
            {  
               "Id":1,
               "Score":50,
               "Rank":1
            },
            {  
               "Id":0,
               "Score":40,
               "Rank":2
            }
         ],
         "Players":[  
            {  
               "Player":{  
                  "Gamertag":"Karl2177",
                  "Xuid":null
               },
               "TeamId":0,
               "Rank":6,
               "Result":1,
               "TotalKills":10,
               "TotalDeaths":13,
               "TotalAssists":1,
               "PreMatchRatings":null,
               "PostMatchRatings":null
            }
         ],
         "IsTeamGame":true,
         "SeasonId":"hex"
      }, ...
    ]
}
 
I've played around with React.js some over the last couple of days and I kinda like it. However, I'm still confused about Javascript modules and package management and bundling and all that. My current Javascript workflow is "put script tags into HTML" and be done with it.
However it gets more complicated with React because I want to use the JSX syntax and also a ES6 transpiler (Babel). I tried using a library (React-Router) that kinda didn't work when I just dropped it in as a script tag and every code example I found was using something like the following:
Code:
import { Router, Route, Link } from 'react-router'
There's a lot of stuff and it all seems to do slightly different things. Like npm, which is the Node package manager, but apparently also has client-side Javascript libraries, then there's Bower, which is also a package manager, but different somehow (?), then I looked at Webpack, which seems to be used to bundle resources. I feel lost. Is there an article that just gives me a good overview over what tool does what and ideally gives me some analogues for other languages?

Come join us in Web Development thread if you want to because this thread moves quite fast so it's easy miss these ones.

That import is an ES6/ES2015 type of import declaration so like you said, you either need to transpile it (using Babel for example) or need to have browser that supports those.

Difference between NPM and Bower is that Bower is meant for frontend packages, but due how JavaScript apps are now built it's quickly becoming a bit obsolete: you can install your packages with NPM and then bundle them for browser with Browserify, Webpack or Rollup. Why does there need to be 3 of different ones? They have many similarities, but also differ in their complexity. Just pick one and use that, it won't be hard to switch around later.

Then comes the part where people come in and say that "oh JS was soooo much better when you didn't need all these tools blah blah" but ignore that noise and do the one time setup (if you are already transpiling JSX files you have most of these done already).

1. Install Node JS (which installs npm)
2. Create your application folder
3. Open your console
4. Install react and react-router (npm install react react-dom react-router)
5. Create foo.jsx
6. insert the import statement (or just follow this: https://github.com/rackt/react-router)
7. Transpile the file with the tool of your choice. https://babeljs.io/docs/setup/#babel_cli
Check this document for pretty much every example. I myself use Grunt + Browserify (with Babelify), but something else might fit you better. And like I said, when you understand the concept, it's easy to switch around when needed.
 
Top Bottom