Input in the same line with space. - c++

I need to do a program and the input is a set of data with 6 lines and with 3 numbers separated by spaces, with a 'c' or with a 'f' in the last line:
input:
4 5 6
7 8 9
7 8 9
4 5 6
7 8 9
7 8 9
c
4 5 6
7 8 9
7 8 9
4 5 6
7 8 9
7 8 9
c
4 5 6
7 8 9
7 8 9
4 5 6
7 8 9
7 8 9
f
If the last letter after the 6 lines is a c, continue with other 6 lines and if it is a f, finish.
I intend with:
int main(){
char a;
int triangulo[6][3];
do{
for(int i=0; i<6; i++){
cin>>triangulo[i][0]>>triangulo[i][1]>>triangulo[i][2];
}
cin>>a;
}while(a != 'f');
system("pause");
}
But it now works, can you help me.
Because I do not how can I recieve 3 numbers in the same line, separated by spaces and receive very much data set.
And another question. For example, if the user write c 7 times, I need create an array for each data set??
input:
4 5 6
7 8 9
7 8 9
4 5 6
7 8 9
7 8 9
c
4 5 6
7 8 9
7 8 9
4 5 6
7 8 9
7 8 9
c
4 5 6
7 8 9
7 8 9
4 5 6
7 8 9
7 8 9
f
And if I have to sum each array and the sum is my output, I need to create an array for each data set??
output
126
126
126

Something like this should work for reading three digits.
cin>>triangulo[0] >> triangulo[1] >> triangulo[2];
However, if you are reading numbers (1, 2, 3, 18, 33, 418 or some such), you probably want to use int triangulo[3]; instead. And if you actually want to store 6 sets of three numbers, you will need a two-dimensional array:
int triangulo[6][3];
...
cin>>triangulo[i][0] >> triangulo[i][1] >> triangulo[i][2];

try this?
int main(){
char a;
int triangulo[3];
int sum=0;
do{
for(int i=0; i<6; i++){
cin>>triangulo[0] >> triangulo[1] >> triangulo[2];
sum=sum+triangulo[0]+triangulo[1]+triangulo[2];
}
cin>>a;
}while(a != 'f');
cout<<sum;
system("pause");
}
If you wish to store all the arrays, you can make the user to input a number(how many arrays will there be) before input the arrays and allocate the memory using new.

Related

Getting funny output for insertion sort

I'm trying to implement insertion sort. My logic may be wrong because I was unable to complete my code due to some error.
I want help with values changing absurdly while executing. Also, there is a similar repeating element question but it is in python and it went over my head. so, please don't mark it duplicate.
As you can see I have initialized a temporary variable index, why you ask? because the value of N is changing during run time.
secondly, Value is getting repeated when sorting is taking place.
I'm using codeblocks 17.2.
#include<iostream>
#include<utility>
#include<algorithm>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int arr[100];
int N,index;
cin>>N;
for(int i=0;i<N;i++)
{
cin>>arr[i];
}
index=N; // using temperory variable
for(int l=0;l<index;l++)
{
for(int j=l+1;j>=0;j--)
{
if(l==index-1 || j==0) //Working fine now
break;
if(arr[j]<arr[j-1])
{
swap(arr[j],arr[j-1]);
}
}
cout<<N<<endl; //value of n is changing but why
for(int k=0;k<index;k++)
{
cout<<arr[k]<<" "; //value of array is also coming wrong
}
cout<<"\n";
}
return 0;
}
N=7
and elements of the array to be
7 8 5 2 4 6 3
output is
7 //these are the values of N which is changing
7 8 5 2 4 6 3
5
7 7 8 2 4 6 3
2
5 7 7 8 4 6 3
2
4 5 7 7 8 6 3
2
4 5 6 7 7 8 3
2
3 4 5 6 7 7 8
0
2 3 4 5 6 7 7
check for boundary condition and when non-existing array index is accessed it will give undefined behavior. In this case, it appears that N was stored right before arr and it changed when you modified arr[-1].

Why is srand(time(NULL)) working smoothly even though I repeatedly reset it?

I have a function that creates a vector of size N, and shuffles it:
void rand_vector_generator(int N) {
srand(time(NULL));
vector <int> perm(N);
for (unsigned k=0; k<N; k++) {
perm[k] = k;
}
random_shuffle(perm.begin(),perm.end());
}
I'm calling this from my main function with the loop:
for(int i=0; i<20; i++)
rand_vector_generator(10);
I expected this to not give me sufficient randomness in my shuffling because I'm calling srand(time(NULL)); with every function call and the seed is not too different from successive call to call. My understanding is that I call srand(time(NULL)); once and not multiple times so the seed doesn't "reset".
This thread somewhat affirms what I was expecting the result to be.
Instead, I get:
6 0 3 5 7 8 4 1 2 9
0 8 6 4 2 3 7 9 1 5
8 2 4 9 5 0 6 7 1 3
0 6 1 8 7 4 5 2 3 9
2 5 1 0 3 7 6 4 8 9
4 5 3 0 1 7 2 9 6 8
8 5 2 9 7 0 6 3 4 1
8 4 9 3 1 5 7 0 6 2
3 7 6 0 9 8 2 4 1 5
8 5 2 3 7 4 6 9 1 0
5 4 0 1 2 6 8 7 3 9
2 5 7 9 6 0 4 3 1 8
5 8 3 7 0 2 1 6 9 4
7 4 9 5 1 8 2 3 0 6
1 9 2 3 8 6 0 7 5 4
0 6 4 3 1 2 9 7 8 5
9 3 8 4 7 5 1 6 0 2
1 9 6 5 3 0 2 4 8 7
7 5 1 8 9 3 4 0 2 6
2 9 6 5 4 0 3 7 8 1
These vectors seem pretty randomly shuffled to me. What am I missing? Does the srand call somehow exist on a different scope than the function call so it doesn't get reset every call? Or am I misunderstanding something more fundamental here?
According to standard the use of std::rand in both std::random_shuffle and std::shuffle is implementation-defined (though it is often the case that an std::rand is used this is not guaranteed). Try it on another compiler? Another platform?
If you want to make sure the std::rand is used you should let your code use it explicitly (for example, using lambda expression):
random_shuffle(perm.begin(), perm.end(), []{return std::rand();});
On a somewhat unrelated note, the time()'s precision is one whole second, your code runs way faster than that (I would hope) so those multiple calls to srand() result in resetting to the same-ish seed

Codechef is rejecting my solution

I am newbie on codechef and i was trying to solve the following question however my code runs fine on my machine, i also tested it with some cases.
Question is as follows :-
In Byteland it is always the military officer's main worry to order his soldiers on parade correctly. Luckily, ordering soldiers is not really such a problem. If a platoon consists of n men, all of them have different rank (from 1 - lowest to n - highest) and on parade they should be lined up from left to right in increasing order of rank.
Sounds simple, doesn't it? Well, Sgt Johnny thought the same, until one day he was faced with a new command. He soon discovered that his elite commandos preferred to do the fighting, and leave the thinking to their superiors. So, when at the first rollcall the soldiers lined up in fairly random order it was not because of their lack of discipline, but simply because they couldn't work out how to form a line in correct order of ranks. Sgt Johnny was not at all amused, particularly as he soon found that none of the soldiers even remembered his own rank. Over the years of service every soldier had only learned which of the other soldiers were his superiors. But Sgt Johnny was not a man to give up easily when faced with a true military challenge. After a moment's thought a solution of brilliant simplicity struck him and he issued the following order: "men, starting from the left, one by one, do: (step forward; go left until there is no superior to the left of you; get back in line).". This did indeed get the men sorted in a few minutes. The problem was solved... for the time being.
The next day, the soldiers came in exactly the same order as the day before, and had to be rearranged using the same method. History repeated. After some weeks, Sgt Johnny managed to force each of his soldiers to remember how many men he passed when going left, and thus make the sorting process even faster.
If you know how many positions each man has to walk to the left, can you try to find out what order of ranks the soldiers initially line up in?
Input
The first line of input contains an integer t<=50, the number of test cases. It is followed by t test cases, each consisting of 2 lines. The first line contains a single integer n (1<=n<=200000). The second line contains n space separated integers wi, denoting how far the i-th soldier in line must walk to the left when applying Sgt Johnny's algorithm.
Output
For each test case, output a single line consisting of n space separated integers - the ranks of the soldiers, given from left to right in their initial arrangement.
Example
Input:
2
3
0 1 0
5
0 1 2 0 1
Output:
2 1 3
3 2 1 5 4
Warning: large Input/Output data, be careful with certain languages
#include <iostream>
#include <string.h>
using namespace std;
int main ()
{
int t,n;
cin >> t;
while(t>0){
cin >> n;
int array[n+1];
int stepsmoved,i;
for(i = 1; i <= n; i++){
array[i] = i;
}
for(i = 1; i <=n; i++){
cin >> stepsmoved;
if(stepsmoved == 0){}
else{
int x;
x = array[i];
for (int j = i; j> i- stepsmoved; j--){
array[j] = array[j-1];
}
array[i-stepsmoved] = x;
}
}
for(i = 1; i <= n; i++){
cout<<array[i]<<" ";
}
cout<<endl;
t--;
}
return 0;
}
So is there something logically or syntactically wrong?
The order of 'unwinding' the sorting is relevant.
Here is the code that demonstrates the statement above (the ranks are 1-based, the 1 - is highest, 10 - is lowest, array indices are 0-based):
#include <stdio.h>
void dump(int *a) {
int i;
for (i = 0; i < 10; i++)
printf("%d ", a[i]);
printf("\n");
}
int main() {
int array[10] = {0}, steps[10] = {0};
int i,j;
srand(0);
// Assign ranks in random order
for (i = 0; i < 10;) {
j = rand() % 10;
if (!array[j])
array[j] = ++i;
}
dump(array);
// Sort according to the Sgt Johnny's initial idea
for (i = 1; i < 10; i++) {
for (j = 0; array[j] < array[i]; j++);
if (j < i) {
int k, temp = array[i];
for (k = i; k > j; k--) {
array[k] = array[k-1];
steps[temp-1]++;
}
array[j] = temp;
dump(array);
}
}
printf("Steps:\n");
dump(steps);
printf("\n");
// reconstruct the origina order
#if 1
for (i = 10-1; i >= 0; i--)
#else
for (i = 0; i < 10; i++)
#endif
{
int s = steps[array[i]-1];
for (j = i; s; s--, j++) {
int temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
dump(array);
}
}
If the reconstruction is done in reverse order, then we get a sequence that matches original:
8 7 5 1 10 4 2 3 9 6
7 8 5 1 10 4 2 3 9 6
5 7 8 1 10 4 2 3 9 6
1 5 7 8 10 4 2 3 9 6
1 4 5 7 8 10 2 3 9 6
1 2 4 5 7 8 10 3 9 6
1 2 3 4 5 7 8 10 9 6
1 2 3 4 5 7 8 9 10 6
1 2 3 4 5 6 7 8 9 10
Steps:
3 5 5 4 2 4 1 0 1 0
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 10 9
1 2 3 4 5 6 7 8 10 9
1 2 3 4 5 6 8 7 10 9
1 2 3 4 5 8 7 10 9 6
1 2 3 4 8 7 5 10 9 6
1 2 3 8 7 5 10 4 9 6
1 2 8 7 5 10 4 3 9 6
1 8 7 5 10 4 2 3 9 6
8 7 5 1 10 4 2 3 9 6
Otherwise, the reconstructed order does not match the original:
8 7 5 1 10 4 2 3 9 6
7 8 5 1 10 4 2 3 9 6
5 7 8 1 10 4 2 3 9 6
1 5 7 8 10 4 2 3 9 6
1 4 5 7 8 10 2 3 9 6
1 2 4 5 7 8 10 3 9 6
1 2 3 4 5 7 8 10 9 6
1 2 3 4 5 7 8 9 10 6
1 2 3 4 5 6 7 8 9 10
Steps:
3 5 5 4 2 4 1 0 1 0
2 3 4 1 5 6 7 8 9 10
2 4 1 5 6 7 3 8 9 10
2 4 5 6 7 1 3 8 9 10
2 4 5 7 1 3 8 6 9 10
2 4 5 7 3 8 6 1 9 10
2 4 5 7 3 8 6 1 9 10
2 4 5 7 3 8 1 9 10 0
2 4 5 7 3 8 1 10 9 0
2 4 5 7 3 8 1 10 0 9
2 4 5 7 3 8 1 10 0 6

How to program all the possible for answer [][]/[] = [][]/[] = [][]/[] with no repeat number from 1 to 9? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
This is a program language was written by C++, fill all the number from 1 to 9 without repeat in the equation [][]/[] = [][]/[] = [][]/[]. I did some tests but seems the answers are not completely, e.g. 57/6 = 19/2 = 38/4, can anyone help? thanks.
#include <iostream>
#include <cmath>
#include <vector>
using namespace std;
/* [][]/[]=[][]/[]=[][]/[] */
bool equal(vector<double> a)
{
double exc = 0.001;
if (fabs((a[0]*10+a[1])/a[2] - (a[3]*10+a[4])/a[5]) > exc)
return false;
if (fabs((a[0]*10+a[1])/a[2] - (a[6]*10+a[7])/a[8]) > exc)
return false;
return true;
}
int main()
{
vector<double> a;
double data=1.0;
for (int i=1; i<=9; ++i)
{
data = i*1.0;
a.push_back(data);
}
while (next_permutation(a.begin(), a.end()))
{
if (equal(a))
{
for (vector<double>::iterator it=a.begin(); it!=a.end(); ++it)
cout << *it << " ";
cout << endl;
}
}
return 0;
}
output:
1 9 2 3 8 4 5 7 6
1 9 2 5 7 6 3 8 4
2 1 3 4 9 7 5 6 8
2 1 3 5 6 8 4 9 7
2 7 3 5 4 6 8 1 9
2 7 3 8 1 9 5 4 6
3 8 4 1 9 2 5 7 6
3 8 4 5 7 6 1 9 2
4 9 7 2 1 3 5 6 8
4 9 7 5 6 8 2 1 3
5 4 6 2 7 3 8 1 9
5 4 6 8 1 9 2 7 3
5 6 8 2 1 3 4 9 7
5 6 8 4 9 7 2 1 3
5 7 6 1 9 2 3 8 4
5 7 6 3 8 4 1 9 2
8 1 9 2 7 3 5 4 6
8 1 9 5 4 6 2 7 3
Maybe there are severl answers in different sequence, you can filter by necessary.

C++ cin numbers ignores first line?

I've run into a really strange issue. I can reproduce on my win7 laptop as well as an ubuntu machine.
I have a C++ program like so:
#include <string>
#include <sstream>
#include <iostream>
using namespace std;
int main() {
for (int i = 0; i < 9; i++) {
string line;
getline(cin, line);
stringstream ss(line);
for (int j = 0; j < 9; j++) {
int p = 8;
ss >> p;
cout << p;
}
cout << endl;
}
return 0;
}
Now, if i compile it an run it with ./a.out < test.txt where text.txt contains:
1 2 3 4 5 6 7 8 9
2 2 3 4 5 6 7 8 9
3 2 3 4 5 6 7 8 9
4 2 3 4 5 6 7 8 9
5 2 3 4 5 6 7 8 9
6 2 3 4 5 6 7 8 9
7 2 3 4 5 6 7 8 9
8 2 3 4 5 6 7 8 9
9 2 3 4 5 6 7 8 9
It will output (without spaces):
8 8 8 8 8 8 8 8 8
2 2 3 4 5 6 7 8 9
3 2 3 4 5 6 7 8 9
4 2 3 4 5 6 7 8 9
5 2 3 4 5 6 7 8 9
6 2 3 4 5 6 7 8 9
7 2 3 4 5 6 7 8 9
8 2 3 4 5 6 7 8 9
9 2 3 4 5 6 7 8 9
Why is the first line wrong? I've tried reading the first line out of the loop as well.
Also, if I replace ss > p with cin > p I just get an output table full of 8's.
This is not making any sense!!
Okay you guys were right. Some weird stuff as the first character of my input file:
od -c test.txt
0000000 357 273 277 2 0 5 0 0 7 0
0000020 0 6 \n 4 0 0 9 6 0
0000040 0 2 0 \n 0 0 0 0 8
It's a problem with the data (since the code looks OK). Most probably you've saved your text file with UTF-8 encoding with BOM. An UTF-8 BOM is three bytes at the start of the file, and trying to interpret those as a decimal number specification would fail.
Second, third, fourth line etc. OK because you're creating new istringstream object for each line, so not retaining error mode from previous line.
So, fix: save the file without BOM -- assuming the BOM hypothesis is correct.
Cheers & hth.,
Your code seems fine to me, if I were you I'd double check the input file : are you sure there is no empty first line, or some non-numeric character on the beginning of line 1 ?
I suspect you wrote your own getline(), and the bug is there. InputStreams have a getline(char*, int), and I suspect your cramming string.begin() into the first param, and Some Other Number into the latter.
Don't do that.
All your program should be doing is copying the input to the output (given this code and that input). It's not doing that either, even on the lines that "work".
I am seeing a number of Not So Experienced Programmer 'signatures' here.
1) Overly short variable names (outside a for loop counter), "ss" and "p"
2) Magic error number (8), particularly one that doesn't stand out from the data.
3) "using"
1 and 3 both hint at a lack of typing speed, and therefore experience... despite your 1k+ reputation (which is based mostly on asking questions... the situation becomes clearer).
I'd rewrite it something like this:
int curDig;
curLine >> curDig;
if (curLine.good()) {
cout << curDig;
} else {
cout << "FAILED at line: " << lineIdx << " containing: " << line << std::endl;
}
Chances are, you're going to see "FAILED at line: 0 containing: " right out of the gate, due to what I think is a bug in your getline().