c++ output formatting problems - c++

I have a list of names in a 2-D array. My program performs some computations and assign these names their respective marks. I am supposed to send the output to a file but I am facing some problems. The problem is the length of each name in the array varies and it has affected my output format. I am looping through the array to print out the names and their values. The output comes nicely when I omit the name.
Name of Pupils/Students Indiv-Test Group Work Class T.
4 3 2
6 15 12
But as soon as I bring the names, then the output changes.
Name of Pupils/Students Indiv-Test Group Work Class T.
1.nana yaw kwadwo 15 14 13 1
2.kwaku 12 15 15
3.osei kofi 12 14 1
So it is the length of the names that affect my output format. Please is there any way, I can get a nice output format like my first sample output together with the names; like this. Still maintaining the length of my names. Highly required.Thank you in advance.
Name of Pupils/Students Indiv-Test Group Work Class T.
1.nana yaw kwadwo 4 3 2
2.kwaku 6 15 12
3.osei kofi 12 14 1
I have tried every output format I know but yet still.
This is my sample code;
for (int n=0;n<=num;n++)
{
name_output<<n+1<<"."<<sample[n];
name_output<<setw(37)<<s.indiv_test[n]<<setw(16)<<s.grp_work[n]<<setw(17) <<s.class_test[n]
}

Related

How do I store numbers in two arrays with specific order?

I want to open a text file and read it in its entirety while storing its contents into arrays using c++. I have my example text file below. I would like to store the first number into an array and the rest of them into a 2nd array line by line. For example 9 to be stored in first array and 22 22 at second array,then 1 to be stored at first array 2 3 4 at second array etc...I am not sure how to accomplish this in c++, any help is greatly appreciated!
9 22 22
1 2 3 4
1 5
2 3 6 9
For example when I print the first array i want to show: 9 1 1 2 (first column)
and when I print the second array I would like to show: 22 22 2 3 4 etc...
Here's one approach:
Create a loop goes on for as long as you can successfully read a line from the file. You'll need a std::string and std::getline for that.
In the loop:
Put the line you read in an std::istringstream to simplify the extraction.
Declare a temporary variable to use for extracting the numbers.
Try to extract a number from the istringstream.
If you successfully extracted a number:
Put the extracted number in arr1.
Create another loop where you extract all the rest of the numbers one by one from the istringstream and put them in arr2.

bash sort so that results are in numeric order as well as string order

Let's say I was comparing two adjacent lines to each other after running sort -u on a file. I find they both match n-characters over from the left side, then begin to disagree at some point, and where the disagreement begins, the first line had a digit "0" to "9". The second line has a non-digit. I want the two lines to swap positions. Why do I want this? Because the digit in the first line meand it is a longer number, and needs to go behind the other, so that these lines, regardless of the digit value, will rearrange from this:
xxxx-xxxx-xxxxxxx.xxxxxxx.xxxx.DD-xx.x.x.x
xxxx-xxxx-xxxxxxx.xxxxxxx.xxxx.D-xx.x.x.x
to this:
xxxx-xxxx-xxxxxxx.xxxxxxx.xxxx.D-xx.x.x.x
xxxx-xxxx-xxxxxxx.xxxxxxx.xxxx.DD-xx.x.x.x
And this:
1
10
11
12
13
14
15
2
3
4
5
6
7
8
9
becomes this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
because it forces numeric values with the same number of digits to be
compared with each other, as those grouped from the left with more digits are moved behind those with fewer digits.
My logic might break down at some point, but until I can code it, I can't check the results returned. So does anybody know how to do this in bash?
sort -g (general numeric sort) should do the trick.

Part of a sorted array is reversed

Given a sorted array, a part of which is reversed. We are required to completely sort it.
eg :
ip - 2 4 5 7 13 11 9 14 19
op - 2 4 5 7 9 11 13 14 19
It's easy to solve when we know whether the given array is sorted in ascending or descending order.If we don't know the order, how to solve it?
eg:
ip - 19 17 2 6 8 10 1
op - 19 17 10 8 6 2 1
also ambiguity occurs when 1st part of input is in ascending order and remaining is in descending order.In that case any order can be considered for output.
Assume the final result should be ascending. Run your algorithm once.
Check whether the output is really ascending. If yes, we are done; if no, go to 3.
Roll back to original input, assume the final result should be descending, run your algorithm once.
Check whether the output is really descending. If yes, we are done; if no, the input is illegal.

MATLAB IF VALUE LESS THAN

Hi I am writing a matlab code at the moment. I am trying to compare the values in a list to the number 10 and if the value is less than 10 add 1 to the total. However I cannot seem to get the code right. My code so far
tot = 0
for i=1:n
if(x(i)<10)
tot = +1
else
y=0;
end
end
tot
The value I get for tot always = 1 and never increases? Can someone help edit this or if not provide a solution to the problem?
I would agree with the answer mentioned above, that one should avoid for loops for this. There can be a faster solution. Since, he is just interested in the counts, and not value of numbers, so there is no need to index things back.
Given:
a = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15]
Computing numbers less than 10 (you could put any number here)
answer = sum(a<10);
Good luck!
In languages like MATLAB and R, you really should not use for loops like this, even as an exercise. Each variable can be a vector, and operations can occur on the whole vector at once, rather than element-by-element.
Given:
x = [ 1 2 3 4 11 12 13 14 15 16 ]
To generate a list of all x less than 10 you would say:
x(x<10)
So to count them:
total = length(x(x<10))
No loop needed or wanted!

Huffman code with lookup table

I have read an article on Internet and know that the natural way of decoding by traversing from root but I want to do it faster with a lookup table.
After reading, I still cannot get the points.
ex:
input:"abcdaabaaabaaa"
code data
0 a
10 b
110 c
111 d
The article says that due to variable length, it determine the length by taking a bit of string of
max code length and use it as index.
output:"010110111001000010000"
Index Index(binary) Code Bits required
0 000 a 1
1 001 a 1
2 010 a 1
3 011 a 1
4 100 b 2
5 101 b 2
6 110 c 3
7 111 d 3
My questions are:
What does it means due to variable length, it determine the length by taking a bit of string of
max code length? How to determine the length?
How to generate the lookup table and how to use it? What is the algorithm behind?
For your example, the maximum code length is 3 bits. So you take the first 3 bits from your stream (010) and use that to index the table. This gives code, 'a' and bits = 1. You consume 1 bit from your input stream, output the code, and carry on. On the second go around you will get (101), which indexes as 'b' and 2 bits, etc.
To build the table, make it as large as 1 << max_code_length, and fill in details as if you are decoding the index as a huffman code. If you look at your example all the indices which begin '0' are a, indices beginning '10' are b, and so on.