03.12.2012 Views

C++ for Scientists - Technische Universität Dresden

C++ for Scientists - Technische Universität Dresden

C++ for Scientists - Technische Universität Dresden

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

62 CHAPTER 2. <strong>C++</strong> BASICS<br />

2.12 Exercises<br />

2.12.1 Age<br />

Write a program that asks input from the keyboard and prints the result on the screen and a<br />

file. The question is: What is your age?<br />

2.12.2 Exercise on include<br />

We provide you the following files: foo.hpp included by bar1.hpp and bar2.hpp. The main<br />

program is in main.cpp.<br />

Compile and try to link the program. It should not link. Correct errors so that it links.<br />

2.12.3 Arrays and pointers<br />

1. Write the following declarations: pointer to a character, array of 10 integers, pointer to<br />

an array of 10 integers, pointer to an array of character strings, pointer to pointer to a<br />

character, integer constant, pointer to an integer constant, constant pointer to an integer.<br />

Initialize all of the objects.<br />

2. Read a sequence of double’s from an input stream. Let the value 0 define the end of a<br />

sequence. Print the values in the input order. Remove duplicate values. Sort the values<br />

be<strong>for</strong>e printing.<br />

3. Make a small program that creates arrays on the stack (fixed size arrays) and arrays on<br />

the heap (using allocation, i.e. new). Use valgrind to check what happens when you do<br />

not use delete correctly.<br />

2.12.4 Read the header of a Matrix-Market file<br />

The Matrix Market data <strong>for</strong>mat is used to store dense and sparse matrices in ASCII <strong>for</strong>mat.<br />

The header contains some in<strong>for</strong>mation about the type and the size of the matrix. For a sparse<br />

matrix, the data are stored in three columns. The first column is the row number, the second<br />

column the column number, and the third column the numerical value. If the matrix is complex,<br />

a fourth column is added <strong>for</strong> the imaginary part.<br />

An example of a Matrix Market file is:<br />

%%MatrixMarket matrix coordinate real general<br />

%<br />

% ATHENS course matrix<br />

%<br />

2025 2025 100015<br />

1 1 .9273558001498543E-01<br />

1 2 .3545880644900583E-01<br />

...................

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!