473,395 Members | 1,496 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

What is the C++ Implementation for This?

I have a Windows text file with the following info inside it:

25 05 38 26 53 04
07 45 50 33 19 34
55 25 21 30 09 39
26 11 30 12 13 41
32 23 44 11 50 39
45 30 07 44 55 54
21 10 35 46 48 27
52 41 05 53 11 50
40 38 17 43 10 54
45 27 29 12 39 31
24 42 38 02 18 09
13 43 28 06 53 30
45 47 29 30 53 13
38 45 28 48 47 36
25 34 18 06 07 55


How can I code them to break that info apart into six columns and put each column into their own array? (Like in the image below...) Kindly include pseudo-code please - so as to guide me at least...
Attached Images
File Type: jpg Untitled.jpg (66.7 KB, 120 views)
Feb 5 '21 #1
7 15201
Banfa
9,065 Expert Mod 8TB
Using a standard stream if you just keep reading integers it should skip the white space in between, including newlines, so just keep reading integers and keep track of which array to add them to. Since this is C++ prefer vectors over arrays.

Expand|Select|Wrap|Line Numbers
  1. vector<vector<integer> > dataStore; // Vector of vectors
  2.  
  3. integer nextVector = 0;
  4.  
  5. while not EOF
  6. {
  7.   Read Integer from File as i
  8.  
  9.   dataStore[nextVector].push_back(i);
  10.  
  11.   nextVector++;
  12.   if (nextVector == 6)
  13.     nextVector = 0;
  14. }
  15.  
  16. Foreach dataStore As entry
  17.   Foreach entry As value
  18.     print value;
  19.  
Feb 5 '21 #2
dev7060
636 Expert 512MB
How can I code them to break that info apart into six columns and put each column into their own array? (Like in the image below...) Kindly include pseudo-code please - so as to guide me at least...
Using arrays
Expand|Select|Wrap|Line Numbers
  1. integer arr[m][n], i = -1, j = 0
  2. while (!EOF)
  3.   arr[j % m][(j++ % m) ? i : ++i] = value read from file
Feb 5 '21 #3
Banfa
9,065 Expert Mod 8TB
That is a a really good attempt and writing in a single line what you should be using multiple lines for; I see the following problems
  1. i is never reinitialised to 0 at the end of a row, it carries on counting up and quickly goes out of bounds, I think ? i : should be something like ? (i=0) :
  2. (j++ % m) must be wrong otherwise you are going down 1 row and across 1 column in your array for nearly every integer read.
  3. The value n is not used anywhere; this whole thing might work better with a single control variable k = 0; then arr[k / n][k++ % n];
  4. [j % m] is unnecessary, it just causes the beginning of the array to be over-written if the file has more data than you are expecting. This may be intentional but a stop condition and an error message would be better.
  5. While your operations on j in line 3 may not actually be undefined behaviour if you are using C++17 or later that is a poor assumption to make (multiple access to a single variable between sequence points where at least 1 of those accesses changes the value of the variable is undefined behaviour; C++17 added [] as sequence points).

The code is very hard to read, I have written and deleted several errors that weren't errors just from failing to get the operation clear in my head to begin with, more simpler lines of code is nearly always better then fewer more complex lines :D
Feb 8 '21 #4
dev7060
636 Expert 512MB
Thanks for the feedback. I appreciate the insights.

According to the approach I'm using, I think 'i' doesn't need to be reinitialized to 0. Modulo is taking care of the shifting.

'n' is used just for the demonstration of the maximum index value. I didn't want to use a magic number like 15.

Logic behavior may be compiler dependent (shoulda mentioned that). Although, I tested it on a bunch of online compilers. Same result.

I tend to introduce compact one-liners during competitive contests where the test cases are provided respecting a format and the main focus is to solve the problem and pass the test cases. I agree readability does matter especially in a collaborative environment. Thanks for pointing this out. I see this may rather appear complex to others. Will keep this in mind in the future for the sake of better expressiveness.

The algo is solely based on the format depicted by the OP in the post and attachment which appeared to me as a fixed number of rows and columns and the pseudo-code was posted keeping in mind the ideal environment.

Here's a run that I think works well as I expect it to
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. #include <iomanip>
  4.  
  5. using namespace std;
  6.  
  7. int main() {
  8.   int m = 6, n = 15;
  9.   int arr[m][n];
  10.   int i = -1, j = 0, count = 90;
  11.   /*
  12.   Sample inputs:
  13.   90 89 88 87 86 85
  14.   84 83 82 81 80 79
  15.   ...
  16.   12 11 10 9  8  7
  17.   6  5  4  3  2  1
  18.   */
  19.   /*
  20.   OP: "put each column into their own array"
  21.   https://bytes.com/attachments/attachment/10468d1612527460/untitled.jpg
  22.   90 84 ... 12 6 ->  say a[0][0] ... a[0][14]
  23.   89 83 ... 11 5-> say a[1][0] ... a[1][14]
  24.   ...
  25.   85 79 ... 1 -> say a[5][0] ... a[5][14]
  26.   */
  27.   while (count) {
  28.     /* condition checks and required breaks here */
  29.     arr[j % m][(j++ % m) ? i : ++i] = count--;
  30.     //arr[j % m][(j++ % m) ? i : (j = 1, ++i)] = count--; //better
  31.   }
  32.   for (i = 0; i < 6; i++) {
  33.     cout << "Array " << i + 1 << " : ";
  34.     for (j = 0; j < 15; j++) {
  35.       cout << setw(2) << arr[i][j] << " ";
  36.     }
  37.     cout << "\n";
  38.   }
  39.   return 0;
  40. }
O/P:

Attached Images
File Type: png cap.PNG (41.2 KB, 779 views)
Feb 8 '21 #5
Banfa
9,065 Expert Mod 8TB
Excellent, now I see my post was about 70% rubbish, I was clearly not keeping the actual problem in mind as I read your post.

I don't think line 30 is better, it adds complexity without removing the need for the % in the first subscript.

I retract points 1, 2 and 4 that I made unequivocally. They are just wrong.

On point 3, the point I was trying to make n is that it is not used in the algorithm, if the file contains more data than expected then your algorithm will just run off the end of the array. You can simulate this with your test code by just changing line 10 to initialise count to say 100 while leaving line 8 alone.

I accept your point that both this and the fact you have put the algorithm on a single line stem from your competition coding; coding for production and coding for competition are 2 different things.

Point 5 still stands in its entirety, you may have tested it on several platforms but one of the most tricky things about undefined behaviour is that the system is free to do anything. That includes working as expected right up until you ship it to a customer; so it is important to be able to recognise constructs that produce it or may produce it and avoid them.

<anecdote>
Anecdote About Undefined Behaviour
Early in the 1990's I was working on a SCADA system. One of our customers reported that the computer only worked at one end of their office.

Slightly bemused we requested more details and were told that the system had been working fine for several months, then the client had decided to reorganise the office and as part of that had move our system from the eastern end of the office to the western end (a move of about 15m). When they switched it back on it crashed and continued to consistently crashed every time. So they moved the computer back to the eastern end of the office were upon it started working again without issue.

They proffered the opinion that they thought there was a Ley Line running under the western end of the office effecting operation and we need to harden our software/computer against Ley Lines.

I was dispatched to investigate, equipped with a development machine. And after a day or so's reproduction of the problem and poring over the code base I found this

Expand|Select|Wrap|Line Numbers
  1. void someFunction()
  2. {
  3.    int *p=0;
  4.  
  5.    // 30-40 lines of code that doesn't reference p
  6.  
  7.    *p = 5;
  8.  
  9.    // Another 30-40 lines of code that doesn't reference p
  10. }
  11.  
As you can see this is definitely undefined behaviour (although at the time I didn't know the term) as we are dereferencing a NULL pointer. Back in the 90s dereferencing a NULL pointer like that didn't produce the instant segmentation fault what it would today because of the segmented memory map that existed and the fact that virtual memory spaces didn't exist so there was less checking and verification.

Obviously I removed these 2 lines of code and all was good, Ley Line Hardening done. Back at the office I checked and discovered that they had been in the code for a couple of years with no problems reported.

Obviously the moral of this story is always test your software on top of a Ley Line 😁
</anecdote>
Feb 9 '21 #6
dev7060
636 Expert 512MB
On point 3, the point I was trying to make n is that it is not used in the algorithm, if the file contains more data than expected then your algorithm will just run off the end of the array. You can simulate this with your test code by just changing line 10 to initialise count to say 100 while leaving line 8 alone.
I tried covering the more than expected values aspect; the statement was rather looking complex. A separate conditional would be better.

Point 5 still stands in its entirety, you may have tested it on several platforms but one of the most tricky things about undefined behaviour is that the system is free to do anything. That includes working as expected right up until you ship it to a customer; so it is important to be able to recognise constructs that produce it or may produce it and avoid them.
Kinda sparked a new perspective to look at undefined behavior.

Anecdote About Undefined Behaviour
Interesting. Thanks for sharing.
Feb 10 '21 #7
mh11111
1 Bit
Thanks for the link here. Hope it will work best.
Feb 15 '22 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Eric Bragas | last post by:
What is this? <img src="{$ ImagesDir}/photo.gif"> I KNOW what an HTML image tag looks like. But what do you call that in the file source? Is it like a virtual directory in IIS? It's some type...
0
by: Hai Nguyen | last post by:
Would anybody please tell me what is this error and what i should do I have 5 files: page1.aspx,page1.aspx.cs; page2.aspx.page2.aspx.cs and page3.cs both page1.aspx.cs and page2.aspx.cs...
4
by: David | last post by:
Hi, Buddy, a newbie's question for you guys, In C++, some functions have a return value type "result", what does this mean, I searched on web, but no hint. thanks a lot David
28
by: Madhur | last post by:
Hello what about this nice way to open a file in single line rather than using if and else. #include<stdio.h> void main() { FILE *nd; clrscr();...
6
by: **Developer** | last post by:
What does this mean: External component has thrown an exception. My program crashes after it leaves a subroutine. What I see during debugging is when I press F11 at the End Sub statement
3
by: Bill J | last post by:
For a VB.NET 2003 standard Windows MDI application, is there a way of implementing the "What's This" functionality through the applications main Help menu? All forms in the application have a...
8
by: Matt | last post by:
Hello. I'm struggling to work out what bit of matrix manipulation the following piece of code is doing to the 3x3 matrix given: #include <stdio.h> int main() { int check_mat = { { 1 , 2...
9
by: JoeC | last post by:
m_iWidth = (int)pBitmapInfo->bmiHeader.biWidth; m_iHeight = (int)pBitmapInfo->bmiHeader.biHeight; What does this mean? I have seen v=&var->member.thing; but what does it mean when you...
92
by: Heinrich Pumpernickel | last post by:
what does this warning mean ? #include <stdio.h> int main() { long l = 100; printf("l is %li\n", l * 10L);
19
by: Eric | last post by:
What is this? ( char * ) &( ( struct aStruct * ) 0 ) It looks like it's taking the address of something that contains a pointer to null, and casting it to a pointer to char. (Actually,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.