This category contains 254 posts

C++: Memory Allocation

Show how to explicitly allocate and deallocate blocks of memory in your language. Show access to different types of memory (i.e., heap, stack, shared, foreign) if applicable. While the C allocation functions are also available in C++, their use is discouraged. Instead, C++ provides new and delete for memory allocation and deallocation. Those function don’t … Continue reading »

Why Is Printing “B” Dramatically Slower Than Printing “#”?

I generated two matrices of 1000 x 1000: First Matrix: O and #. Second Matrix: O and B. Using the following code, the first matrix took 8.52 seconds to complete: Random r = new Random(); for (int i = 0; i < 1000; i++) { for (int j = 0; j < 1000; j++) { … Continue reading »

C++: Write CSV File

void write_csv_file(double A[], int length, string file_name) //Function that prints array A to a *.csv file { ofstream output(file_name); for(int i = 0; i < length; i++) { output << A[i] << endl; } }

C++: Map Range

Given two ranges, [a1,a2] and [b1,b2]; then a value s in range [a1,a2] is linearly mapped to a value t in range [b1,b2] when: The task is to write a function/subroutine/… that takes two ranges and a real number, and returns the mapping of the real number from the first to the second range. Use … Continue reading »

C++: Radix Sort

// C++ LSD Radix Sort example, queue implementation #include <iostream.h> #include <cstdlib.h> #include <ctime.h> using std::cout; // Remove this line for older C++ compilers typedef struct slist_ { int val; struct slist_ *next; } slist; slist *radixsort(slist *L, int t) { int i, j, d, m=1; slist *temp, *head[10], *tail[10]; // Process t digits for … Continue reading »

C++ Graphs

Beginner’s Guide to C++

Introduction Getting Started Hello World Dissecting “Hello World” Directives Namespaces Entry Streams The Language Variables Types Operators Flow Control Functions Classes Pointers An Application Adding Tasks: Files and Vectors Listing Tasks Marking Tasks as Done Sorting Tasks: Overridden Operators and STL Algorithms Conclusion Introduction C++ is a general purpose programming language. Anyone already programming at … Continue reading »

An Introduction to the Imperative Part of C++

This document is part of the HTML publication “An Introduction to the Imperative Part of C++” The original version was produced by Rob Miller at Imperial College London, September 1996. Version 1.1 (modified by David Clark at Imperial College London, September 1997) Version 1.2 (modified by Bob White at Imperial College London, September 1998) Version … Continue reading »

C++: Hostname

Find the name of the host on which the routine is running. #include <stdlib.h> #include <stdio.h> #include <limits.h> #include <unistd.h> int main(void) { char name[_POSIX_HOST_NAME_MAX + 1]; return gethostname(name, sizeof name) == -1 || printf(“%s\n”, name) < 0 ? EXIT_FAILURE : EXIT_SUCCESS; } SOURCE Content is available under GNU Free Documentation License 1.2.

C++: Happy Numbers

From Wikipedia, the free encyclopedia: A happy number is defined by the following process. Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include … Continue reading »

UNCC MFE 300x250

Become a TFE Insider!