I'm working on a a project for my class where I have to use all four algorithms for the maximum subarray sub problem (cubic, quadratic, linear, and recursive). My problem is that I'm supposed to read the input from a file and I'm having trouble figuring out how to read until the end of a line, execute code with that data, and then move onto the next line. The input file looks like this:
2
-5 -10 -2 -4
2
-2 10 -5 -6
3
-10 -5 0 5 -20 20 -50
4
10 8 2 -20 -50 -100 -150
0
-1 -2 -3 -4 -5
1
-100 -200 0
4
200 500 -700 1000 2000 -5000 4 10
4
100 200
6
0 0 0 0 0 0 -10
5
-4 10 -3 200 500 -700 2
0
5 10 15 20 25 30 35
2
10 30 50 70 100
3
-15 16 23 -30 0 -2 13 8 6 0 -4 -15
12
9 8 23 -1 -23 -4 0 0 4 7 9 10 -23 68 1 -2 -3 -6 -19 10 5 1 1 2 4 3 -46 12 -100 78 -23 0 0 12 2 7
5
-1 -3 0 2 3 6 1 -5 -3 -2
now my issue is that I want to be able to read in a single number (the minimum subarray length) and then fill the array with the row of numbers below it, then execute all four algorithms using that data, then I want to move on to the next minimum subarray length and the next array of values below it, execute all four algorithms etc., and I want to be able to do this all in one go.
For example, I want to read 2 as the minimum subarray length, then use (-5,-10,-2,-4) as the array, execute all four algorithms, then use 2 as the minimum length, (-5,10,-5,-6) as the array, execute all four algorithms, and keep doing that until the end of the file.
Here is a quick possible solution that might help ("test.txt" contains your values...)
#include <fstream>
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;
int main()
{
ifstream in("test.txt");
string str;
int value;
while (in)
{
vector<int> vec;
getline(in, str);
stringstream stream(str);
while (stream)
{
stream >> value;
vec.push_back(value);
}
vec.erase(vec.end()-1);
//Here the vec conntains all values of the current line
for (auto v : vec)
cout << v << " ";
cout << endl;
}
}
Related
Hello Friends, I was trying to make Triplet form of a sparse matrix using pointers and DMA but the output in first row is returning garbage values can anyone help me in resolving this issue.
int main(){
int k=0,z=0,arr[6][6]={
{15,0,0,22,0,-15},
{0,11,3,0,0,0},
{0,0,0,-6,0,0},
{0,0,0,0,0,0},
{91,0,0,0,0,0},
{0,0,28,0,0,0}
};
int **trip=new int *[3];
for(int i=0;i<6;i++){
for(int j=0;j<6;j++){
if (arr[i][j]==0){
z++;
}
else{
trip[k]=new int(1);
trip[k][0]=i;
trip[k][1]=j;
trip[k][2]=arr[i][j];
k++;
}
}
}
}
Output-
-65511600 22028 -65511568
0 3 22
0 5 -15
1 1 11
1 2 3
2 3 -6
4 0 91
5 2 28
Expected Output
0 0 15
0 3 22
0 5 -15
1 1 11
1 2 3
2 3 -6
4 0 91
5 2 28
I have the following file stored in a string vector.
ratingsTiny.txt...
Jesse
-3 5 -3 0 -1 -1 0 0 5
Shakea
5 0 5 0 5 0 0 0 1
Batool
5 -5 0 0 0 0 0 -3 -5
Muhammad
0 0 0 -5 0 -3 0 0 0
Maria
5 0 5 0 0 0 0 1 0
Alex
5 0 0 5 0 5 5 1 0
Riley
-5 3 -5 0 -1 0 0 0 3
My goal is extract the numbers, preferably column wise, to add them together and get an average rating for each column among the 7 users.
My closest attempt is below, but I can only print the first column and I can't figure out how to iterate through the entirety of the rows to get the rest of the integers.
Any help is very much appreciated.
ourvector<string> ratings;
for (int i = 1; i < ratings.size(); i += 2){
int num = atoi(ratings[i].c_str());
intRatings.push_back(num);
cout << num << endl;
}
I have a problem with the output of the soft demapper function (demodulate_soft_bits) when using 16-QAM and 64-QAM modulation.
here is a portion of the code (64-QAM):
cvec comple;
Modulator_2D qam;
ivec cont_d = "0:1:63";
vec qam_r = "-7 -7 -7 -7 -7 -7 -7 -7 -5 -5 -5 -5 -5 -5 -5 -5 -3 -3 -3 -3 -3 -3 -3 -3 -1 -1 -1 -1 -1 -1 -1 -1 1 1 1 1 1 1 1 1 3 3 3 3 3 3 3 3 5 5 5 5 5 5 5 5 7 7 7 7 7 7 7 7";
vec qam_i = "-7 -5 -3 -1 1 3 5 7 -7 -5 -3 -1 1 3 5 7 -7 -5 -3 -1 1 3 5 7 -7 -5 -3 -1 1 3 5 7 -7 -5 -3 -1 1 3 5 7 -7 -5 -3 -1 1 3 5 7 -7 -5 -3 -1 1 3 5 7 -7 -5 -3 -1 1 3 5 7";
comple.set_length(qam_i.length(),false);
for(int i=0; i<qam_i.length(); i++)
comple(i) = std::complex<double>(qam_r(i)/sqrt((double)42),qam_i(i)/sqrt((double)42));
qam.set(comple, cont_d);
temp = qam.demodulate_soft_bits( symb_recus,channel,1);
symb_recus is the output of a realistic 4x4 MIMO channel
channel is the channel gain
At the output I got a random values which doesn't correspond to the hard output and I got also a succession of 0
exp :
temp : [6.80 3.33 1.64 -14.27 -7.06 -3.51 0 0 0 0 0 0 -3.47 .....]
Hard output : [ 0 1 0 1 1 1 1 1 0 0 1 1 0 ... ]
can you please help me
Here's an example text I'm trying to format:
(9 12 19 38 0 -39 -20 -13 -10)
(7 9 14 29 0 -30 -15 -10 -8)
(4 6 9 19 0 -20 -10 -7 -5)
(2 3 4 9 0 -10 -5 -4 -3)
(0 0 0 0 0 0 0 0 0)
(-3 -4 -5 -10 0 9 4 3 2)
(-5 -7 -10 -20 0 19 9 6 4)
(-8 -10 -15 -30 0 29 14 9 7)
(-10 -13 -20 -39 0 38 19 12 9)
I would like each column to align on digits, (i.e. if there's a minus sign, add extra space).
If I do it like so: C-uM-xalign-regexpRET\([[:digit:]]+\)RETRETRETy
I get this:
(9 12 19 38 0 -3 -2 -1 -1)
(7 9 14 29 0 -3 -1 -1 -8)
(4 6 9 19 0 -2 -1 -7 -5)
(2 3 4 9 0 -1 -5 -4 -3)
(0 0 0 0 0 0 0 0 0 )
(-3 -4 -5 -1 0 9 4 3 2 )
(-5 -7 -1 -2 0 19 9 6 4 )
(-8 -1 -1 -3 0 29 14 9 7 )
(-1 -1 -2 -3 0 38 19 12 9 )
which is very close, but not what I want.
And if I try to modify the expression to include the minus sign, like so: \(-?[[:digit:]]+\)
Then I get this:
(9 1 1 3 0 - - - -)
(7 9 1 2 0 - - - -)
(4 6 9 1 0 - - - -)
(2 3 4 9 0 - - - -)
(0 0 0 0 0 0 0 0 0)
(- - - - 0 9 4 3 2)
(- - - - 0 1 9 6 4)
(- - - - 0 2 1 9 7)
(- - - - 0 3 1 1 9)
Is this a bug, or is there something I don't know?
The text matched by the designated group (usually group 1) is expanded or shrunk, so non-whitespace characters in the group are subject to deletion, as you saw. Unless, that is, justification is enabled, which is indicated by supplying a negative group number to align-regexp:
C-uM-xalign-regexpRET\(\s-*-?\)[0-9]+RET-1RETRETy
If you want, you can align the columns on the ones digits of each number by including the digits in the match group:
C-uM-xalign-regexpRET\(\s-*-?[0-9]+\)RET-1RETRETy
In either case, an extra space will be inserted after each opening parenthesis. I don't see any way to keep align-regexp from doing this, but if you do it often you could wrap it in a command that does the align-regexp, then replaces the regexp "^( " with "(" everywhere in the original region.
#include<iostream>
#include<time.h>
#include<list>
#include<stdlib.h>
#include<fstream>
using namespace std;
typedef struct diskBtNode
{
int parent; //-1 if NULL
//int size;
int leaf;
int arr[20];
};
int main()
{
fstream myfile;
srand(time(NULL));
myfile.open("btree.txt",ios::in | ios::out | ios::binary | ios::trunc);
long nodesize=256;
long currentpos=0;
if(myfile.fail())
{
std::cout<<"Error opening the file "<<std::endl;
}
currentpos=0;
for(int i=0;i<10;i++)
{
diskBtNode node;
node.parent=rand()%10;
node.leaf=rand()%1;
int n=rand()%19;
int j;
for(j=0;j<n;j++)
{
node.arr[j]=n;
}
node.arr[j]=-1;
cout<<node.parent<<" "<<node.leaf<<" ";
j=0;
while(node.arr[j]!=-1)
{
cout<<node.arr[j]<<" ";
j++;
}
cout<<node.arr[j]<<std::endl;
myfile.seekp(currentpos*nodesize,ios::beg);
myfile.write(reinterpret_cast<char *>(&node),nodesize);
currentpos++;
// p=p+1;
}
cout<<"******************* "<<std::endl;
currentpos--;
long p=0;
while(currentpos>=0)
{
std::cout<<currentpos<<" &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& "<<p<<" "<<std::endl;
diskBtNode node;
myfile.seekg(currentpos*nodesize,ios::beg);
myfile.read(reinterpret_cast<char *>(&node),nodesize);
currentpos--;
p--; //decrementing p
cout<<node.parent<<" "<<node.leaf<<" ";
int j=0;
while(node.arr[j]!=-1)
{
cout<<node.arr[j]<<" ";
j++;
}
cout<<node.arr[j]<<std::endl;
}
myfile.close();
}
This code simply reads and writes to a binary file. In the first part it writes to a file and in the second part it reads from the same file. While reading I was trying to read any random blocks from a file for a finite number of time. But when I am using p variable as a counter, it doesn't work. It's value is decremented in the first iteration directly to -1. I used debugger to track where it changes. Apparently it changes after the read statement is executed. Can somebody please help me with this? The output of the above program is this
8 0 8 8 8 8 8 8 8 8 -1
5 0 8 8 8 8 8 8 8 8 -1
3 0 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 -1
5 0 1 -1
4 0 -1
9 0 13 13 13 13 13 13 13 13 13 13 13 13 13 -1
4 0 11 11 11 11 11 11 11 11 11 11 11 -1
6 0 6 6 6 6 6 6 -1
6 0 8 8 8 8 8 8 8 8 -1
2 0 2 2 -1
*******************
9 &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& 0
2 0 2 2 -1
8 &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& -1
6 0 8 8 8 8 8 8 8 8 -1
7 &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& -1
6 0 6 6 6 6 6 6 -1
6 &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& -1
4 0 11 11 11 11 11 11 11 11 11 11 11 -1
5 &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& -1
9 0 13 13 13 13 13 13 13 13 13 13 13 13 13 -1
4 &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& -1
4 0 -1
3 &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& -1
5 0 1 -1
2 &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& -1
3 0 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 -1
1 &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& -1
5 0 8 8 8 8 8 8 8 8 -1
0 &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& -1
8 0 8 8 8 8 8 8 8 8 -1
The problem comes from this line :
myfile.read(reinterpret_cast<char *>(&node),nodesize);
nodesizeequals 256, while you structure's size if 88byte ( 22 * 4 bytes int ).
The read is writing memory over the structure, which happens to be the other stack variables.
Use sizeof( node ) when you both write and read the struct to the file.
Not clear what you are trying to achieve, but in your code you have specified.
long p=0;
while(currentpos>=0)
{
....
p--; // this will make p = -1
}
so the p will print as -1 all through the while statement. Are you forgetting to initialize the p variable?