Categories
Mastering Development

withoutwith volatile global variable in for loop

I’m new to C, just a question on volatile variables. We know that using volatile,we force the compiler to issue instructions that read variables from main memory instead of reading from cache/registers, below is the c code volatile int count = 0; void foo(int count) { int i; for (i = 0; i < count; […]

Categories
Mastering Development

converting for loop to foreach C++

I’m new to C++, and I was wondering if someone could help me convert the following to for loop into foreach loop ? My object is to explore foreach and for loop performance difference . for (size_t i = 0; i!= n; i++) { for (size_t j = 0; j!= m; j++) { vectortParams[j]=output_parameters [i*m+j] […]

Categories
Mastering Development

how to print hex representation of a data type correctly?

I’m new to C, let’s say I want to print a short int as: short s = 0xbeef; printf(“This is 0x%x”, s); the output is 0xffffbeef and it looks like short has four bytes, but actually short only has two bytes, so why %x cannot print it properly? and if it has a rule that […]

Categories
Mastering Development

Is there a way to calculate the length of an arry inside a function instead of pass in as parameter?

I’m new to C++, and here is a template selection sort function I wrote which takes in an array and it length as parameter. “”” template <class T> void mySort(T a[], int len) { for (int i = 0; i < len; i++) { int min = i; for (int j = i+1; j < […]

Categories
Mastering Development

Value pushed to the stack is not what I pop out C++

I’m new to c++, I’m trying to read values from a text file and push only the integers to a stack. The issue I’m having is that when I do a pop() the value that comes out of the stack is different. For example if I push a 4, when I do a pop it […]

Categories
Development

Convert JavaScript code to C# to get Frequent Word Count

I’m new to c#, I solved this algorithm using Javascript, can anyone help me to convert the below code to c#. Details: Given an array of words and a number K, return an array with length K where the ith element of the array is the number of unique prexes with length i among the […]

Categories
Development

Assigning chars from static char array to dynamically allocated char array – Access violation

I’m new to C++, having some fun with it. And I don’t understand why this code isn’t working and throwing access memory violation exception. It works but when I is 6 it throws the exception What I’m doing here is I’m trying to experiment with 128 position numeral system. Code – #include <iostream> char* Encode(unsigned […]

Categories
Development

Object vs Reference for class members and dependency injection

I’m new to C++, with a C# background. I’m trying to use dependency injection commonly used in C# in C++ and I’m confused about the different ways to declare dependencies and pass them in and why I would use one over the other. Let’s assume A depends on B. Approach #1 – declare as object […]

Categories
Development

Object vs Reference for class members and dependency injection

I’m new to C++, with a C# background. I’m trying to use dependency injection commonly used in C# in C++ and I’m confused about the different ways to declare dependencies and pass them in and why I would use one over the other. Let’s assume A depends on B. Approach #1 – declare as object […]