Printing numbers column by column [closed] - c++

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am trying to print numbers column by column.
I am trying to run 50 simulations that will produce a list of numbers. For every simulation, the length of such list is unknown.
For every simulation I want to print the numbers down a column.
When the next simulation starts I want the program to go back up to the top of the file and start printing the list down without entering into the previous list.
All I can find are ways to print the numbers by row, but I will not know the numbers down the row unless I do all the simulations.
Any advice would be much appreciated!

You have a few choices:
Write each sim result to a separate file and collate later;
Hold all results in memory until finished;
After each simulation, read the existing file, and output it again with the new results added in;
Knowing the number of simulations you intend to produce, write results in binary and leave padding for those that are not yet done.
I would probably go with 1.

By "goes back to the top of the file", do you actually mean rewind the file pointer to access memory starting at the beginning of the file? I will assume that's what you meant. Since you want the first input after the last output, then you can run your simulations and print them in reverse order, or simply call the appropriate library functions as you need them.

Related

Cannot figure out why my functions aren't reading through the whole array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
https://onlinegdb.com/r1jGMHRPO
This is my final project for the class and since it's online, i dont really have many resources that i can get help from so i thought i might post this here. I used a while loop to create an array from the data from a text file "numbers.txt" and made function for lowest, highest, total and average that were later called to display the results but for some reason i keep getting the first number of the array as the answer for all of them. Any help will be appreciated.
You are returning the highest, lowest, etc. as soon as you find the first value that is lower than your predefined min and max values. Move the return; keyword outside of the for loop. Also, you should use int lowest = INT_MAX and int highest = INT_MIN because there could be a number higher than 100k and so on. Also, move the return sum; outside the scope of the totalSum forloop too.
your code work just fine on my environment.
maybe your problem is your numbers.txt file is not at the exe file's location, so the file is not found and eof never reached.
put your numbers.txt file at the exe file's location or you have to check it first when you want to read from it. You can check the way to do it here

If a number of test cases isn't provided, when should my program stop reading input? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm stuck at this problem where I have to run some tests and output the result. It's nothing special but the problem is it says "several test cases".
I mean it doesn't take any variable to fix the number of test cases. I wrote the code for one case and can run it a fixed amount of time with a loop. But how do I know when to stop the code when i don't know how many test cases the online judge is going to run?
There must be some input that starts a set of input. For example an array length/ number of elements or a string. On that thing you have to keep an eye. Just keep a loop like this..
int n;
while(cin>>n)
{
//do something
}
If nothing is mentioned you need not consider this.
In competitive programming in general the number of test cases are mentioned..so then you can just loop over a variable considering the testcase numbers.
Another way to specify end of inputs
There is another way of specifying end of input (Often encountered in UVA online judge ) that is in the last line they give -1 or a string "END". So in those cases you may take values into variables after completing a test case and check whether -1 or "END" is encountered and exit as required.
Example
For example: this problem [From Codeforces] specifies no test cases. Here it is expected that you just get n and the elements and process it and give output. Nothing else. You don't have to consider the testcases as it is not mentioned.Your program will be run multiple times on different input sets.

Generating relative Prime number of a user defined prime number? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
I'm creating a program in C and I need to use relative prime numbers in sequence using an algorithm so that the user can select the first number in the sequence.
So far I have managed to create a function that creates relative prime numbers based on one or more inputted by a user but not one that finds the next smallest relative prime.
Either that or a way to produce the smallest relative prime number to a user defined number would be ideal.
Any ideas?
Also, I cannot get gcd to work so I created my own. Do i have to include a specific library other than math.h and stdio.h?
If you want to find the next smallest relative prime, then I think you need to loop from the user's inputted number (e.g. if users input 3, then you need to loop from 4), and then check whether that number is relatively prime.
To check whether two numbers are relatively prime, you can use gcd, and one very famous algorithm to do this is to use Euclid's algorithm. You don't need to include a specific library, it's basically just looping and doing modulo. Take a look at this link.

Generate prime factors of a number [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm trying to write a function that given an Int greater than one gives a non decreasing list made of the prime factors (with repetition) of that number
Example: n = 12, the output should be [2,2,3]
I don't know where to start.
There are of course well know algorithms for what you want to do, so simple google search would really solve that.
However, I'd like to show you a simple thinking process that might be helpful in the future.
Since the factors have to appear in the ascending order, you might:
Start with the lowest prime (2).
Check if the number can be divided by it. If it can, do it and go back to 1.
If not, replace 2 with a next prime and go back to 2.
Now, it's obvious that the biggest prime you will ever check is the number you've started with. However, the basic multiplication axiom states that if a number can be divided by a:
n / a = b
Then it can also be divided by b! You can use that fact to further narrow the checking range, but I'll leave it to you to figure (or google) the upper bound.
Actual implementation is of course a part of your homework and thus supplying code wouldn't be a wise idea here. However, I don't think that stuff such as next_prime will be hard for you.

Effective method for reading large CSV files? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I currently have 5 CSV files with about 45,000 records per each file. Whats the best method to go about this? Ive done I/O before, but never on this scale. Parse into a vector string?
Yes, read them into a vector would be reasonable.
The choice of storage does depend a little on what you are planning to do, and what the data is. If you know what the format of the data is, you may want to create a struct, and read the data into a more organised form. E.g. if the file is like this:
name, score, err
Mats, 89, 2.1%
Steve, 79, 8%
then you could have a structure like this:
struct Row
{
string name;
int score;
float err;
}
As comments say, 45K lines is not very much, and it shouldn't cause any major problems unless you are running it on something with the computing capacity of a wrist-watch.
Just keep doing what you are doing: read all lines into a Vector of strings, a Vector of a Vector of strings, or a Vector of objects. We are talking 200 to 500 MB RAM, and nowadays most computers have much more than that available. From a processing time point of view, that's going to be 5 to 10 minutes in an average computer (depending on the amount of processing, of course).
If you run into any problem, ask a new question with more information.