Why does my won't my code populate the matrix? - matrix

My code is trying to find the beginning and ending indices of a section of a matrix that when added, together would equal 20. For each instance this occurs, it would then populate a matrix with said beginning and end indices in the format {beginning row index, beginning column index, ending row index, ending column index} for each row. Each row would represent separate instances. It works fine for one instance but when introduced to other instances it wouldn't populate the matrix. Please help.
#include <cstddef> // size_t
#include <iostream>
using namespace std;
// Populates matrix
void filler(int bIndr, int bIndc, int eIndr, int eIndc, size_t**matrix, const size_t kIndices_size2, const size_t kIndices_size) {
int t = 0;
int matrix2[4] = {0,0,0,0};
for(int i = 0 ; i < kIndices_size2; i++) {
for (int j = 0; j < 2; j++) {
for (int ii = t; ii < kIndices_size; ii++) {
if(j == 0) {
matrix2[ii] = bIndr;
matrix2[ii+1] = bIndc;
cout << matrix2[ii+1] << endl;
break;
}
if(j == 1) {
matrix2[ii] = eIndr;
matrix2[ii+1] = eIndc;
cout << matrix2[ii+1] << endl;
break;
}
}
t = 2;
}
}
for(int i = 0 ; i < kIndices_size; i++) {
matrix[kIndices_size2-1][i] = matrix2[i];
}
}
int main()
{
int goal = 20;
int array[2][8] = {{10,0,0,10,0,0,1,0},{0,0,10,0,0,0,10,0}};
int inst = 0;
int t=0;
int bIndr = 0;
int bIndc = 0;
int eIndr = 0;
int eIndc = 0;
const size_t kIndices_size = 4;
size_t**matrix;
for(int ii = 0; ii < 2; ii++) {
bIndc =0;
for(int j = bIndc; j < 8; j++) {
t = 0;
bIndr = ii;
bIndc = j;
for(int i = j; i < 8; i++) {
t += array[ii][i];
if((goal-t) == 0) {
inst++;
eIndc = i;
eIndr = ii;
matrix=new size_t*[inst];
matrix[inst-1]=new size_t [kIndices_size];
cout << bIndr << bIndc << eIndr << eIndc << endl;
filler(bIndr, bIndc, eIndr, eIndc, matrix, inst, kIndices_size);
break;
}
}
}
}
size_t actual_size = static_cast<size_t>(-1);
cout << actual_size << endl;
size_t* sums_found = &actual_size;
*sums_found = inst;
cout << actual_size << endl;
cout << matrix[0][0] << endl;
for(int i = 0; i < inst; i++) {
for(int ii = 0; ii < kIndices_size; ii++) {
cout << matrix[i][ii] << " ";
}
cout << endl;
}
return 0;
}

Related

pyramid with "*" and "." in c++

I need to do a pyramid with stars and dots. The first half of the code is doing correct but the other half is doing wrong. I have tried this.
The input is: size=4 and n = 7
the output is:
*...
**..
***.
****
.***
..**
...*
I want it to be
*...
**..
***.
****
***.
**..
*...
#include < iostream >
using namespace std;
int main() {
int size, n;
cin >> size;
cin >> n;
for (int R = 1; R <= size; R++) {
if (R > n) {
break;
}
for (int i = 1; i <= size; i++) {
if (i > R) {
cout << ".";
} else {
cout << "*";
}
}
cout << endl;
}
for (int R = 1; R <= size - 1; R++) {
if (R > n) {
break;
}
for (int i = 1; i <= size; i++) {
if (i > R) {
cout << "*";
} else {
cout << ".";
}
}
cout << endl;
}
return 0;
}

Program will exit after running a specific function in C++. Unable to figure out why

I have this function that will split the string. After running it in a while loop of read file, the program will exit instead of continue the next step of functions.
vector <string> splitString (string str, string delim){
vector <string> result;
size_t pos = 0;
string token;
while ((pos = str.find(delim)) != std::string::npos){
token = str.substr(0, pos);
result.push_back(token);
str.erase(0, pos+delim.length());
}
if (!str.empty())
result.push_back(str);
return (result);
}
void readCityLocation(int ** cityMap){
ifstream inFile;
inFile.open(filename[0]);
string line;
while (getline(inFile, line)){
int xCoordinate;
int yCoordinate;
string xValue;
string yValue;
//Get xCoordinate...
vector <string> splitThis = splitString(line,",");
xValue = splitThis[0];
cout << line;
}
An function called option2(), will create a table and call void readCityLocation( )
void option2(){
cout << endl << endl;
int ** table = new int * [gridXEnd + 1];
for (int i = 0; i < gridXEnd + 1; i++) {
table[i] = new int[gridYEnd + 1];
}
//Initialize all the array value in table to be 0
for (int i = 0; i < gridXEnd + 1; i++) {
for (int j = 0; j < gridYEnd + 1; j++) {
table[i][j] = 0;
//To be remove: Error Handling
//cout << table[i][j] << " Grid X: " << i << " Grid Y: " << j << endl;
}
}
readCityLocation(table);
}
I am fairly new to all this and can't figure out whats the problem. Any assistance is appreciated. Thank you.

Code just loads for a second, then closes without doing anything

For some reason, whenever I run this code, it just opens; loads for a sec; then closes without doing anything. Whenever I try to narrow it down to a piece of code, it makes absolutely no sense, like the line int dirX.
#include <iostream>
#include <queue>
using namespace std;
void solve()
{
// ENTER CODE BELOW
struct Loc
{
int x, y;
Loc (int xx=0, int yy=0) : x(xx), y(yy) {}
};
int n=0, currX=1002, currY=1002, dx[]={-1,1,0,0},dy[]={0,0,-1,1}; string str=""; bool isFence[2010][2010]; queue<Loc> q;
int ret=-1;
for (int i = 0; i < 2005; i++) {
for (int j = 0; j < 2005; j++) {
isFence[i][j]=false;
}
}
cin >> n >> str;
isFence[currX][currY]=true;
int dirX, dirY;
for (auto i : str)
{
dirX=0; dirY=0;
if (i=='N') dirX=-1;
else if (i=='S') dirX=1;
else if (i=='W') dirY=-1;
else dirY=1;
for (int j = 0; j < 2; j++) {
currX += dirX;
currY += dirY;
isFence[currX][currY]=true;
}
}
Loc curr; int nx, ny;
for (int i = 0; i < 2005; i++)
{
for (int j = 0; j < 2005; j++)
{
cout << isFence[i][j] << endl;
if (isFence[i][j]) continue;
ret++;
q = std::queue<Loc>();
q.push(Loc(i,j));
isFence[i][j]=true;
while (!q.empty())
{
curr = q.front(); q.pop();
for (int k = 0; k < 4; k++) {
nx = curr.x+dx[k]; ny=curr.y+dy[k];
if (nx >= 0 && nx < 2005 && ny >= 0 && ny<2005 && !isFence[nx][ny]) {
isFence[nx][ny]=true;
q.push(Loc(nx, ny));
}
}
}
}
}
cout << ret;
// ENTER CODE ABOVE
}
int main()
{
solve();
}
Also, the reason I have all my code in the solve() function was because this is an assignment and I have to do it this way.
Sidenote: I wrote this code very quickly, so it's very badly formatted.

when gray image width is 2048, 4096, running time of rotate 90 image is more longer than nearby

Now I am going to rotate image 90 degrees, the code is below, using openCV Mat struct.
And I found a phenomenon that when the image width is 4096 ,running time is about twice of running time of image width is 4097. Following is the running time output. Does somebody Know why?
#include<string>
#include<fstream>
#include<iostream>
#include "opencv.hpp"
using namespace cv;
void main()
{
std::string ImageFileName0 = "F:\\temp\\4096-3000.bmp";
std::string ImageFileName1 = "F:\\temp\\4097-3000.bmp";
Mat image4096 = imread(ImageFileName0);
Mat image4097 = imread(ImageFileName1);
int lWidth4096 = image4096.size().width;
int lHeight4096 = image4096.size().height;
Mat image4096Res(lWidth4096, lHeight4096, CV_8UC1);
for (int i=0; i<lHeight4096; i++)
{
for (int j=0; j<lWidth4096;j++)
{
image4096Res.at<uchar>(j, i) = 0;
}
}
int lWidth4097 = image4097.size().width;
int lHeight4097 = image4097.size().height;
Mat image4097Res(lWidth4097, lHeight4097, CV_8UC1);
for (int i = 0; i < lHeight4097; i++)
{
for (int j = 0; j < lWidth4097; j++)
{
image4097Res.at<uchar>(j, i) = 0;
}
}
for (int i = 0; i < 10; i++)
{
double time0 = static_cast<double>(getTickCount());
for (int j = 0; j < lWidth4096; ++j)
{
for (int i = 0; i<lHeight4096; ++i)
{
image4096Res.at<uchar>(j, i) = image4096.at<uchar>(i, j);
}
}
double time4096 = (static_cast<double>(getTickCount()) - time0) / getTickFrequency();
double time1 = static_cast<double>(getTickCount());
for (int j = 0; j < lWidth4097; ++j)
{
for (int i = 0; i<lHeight4097; ++i)
{
//srcTmp = srcimage.PointToRow(i);
//*(dstTmp + i) = srcimage.GetPixelValue(j, i);
image4097Res.at<uchar>(j, i) = image4097.at<uchar>(i, j);
}
}
double time4097 = (static_cast<double>(getTickCount()) - time1) / getTickFrequency();
std::cout << "4096 time:" << time4096*1000 << std::endl;
std::cout << "4097 time:" << time4097*1000 << std::endl;
std::cout << std::endl;
}
namedWindow("aa",CV_WINDOW_NORMAL);
imshow("aa", image4096Res);
waitKey();
}
Running time result:
4096 time:149.337
4097 time:56.8092
4096 time:143.556
4097 time:67.4758
4096 time:142.07
4097 time:58.2825
4096 time:153.973
4097 time:57.1894
4096 time:145.086
4097 time:58.7944
4096 time:156.33
4097 time:87.9404
4096 time:140.224
4097 time:56.9525
4096 time:144.413
4097 time:57.133
4096 time:141.672
4097 time:54.916
4096 time:148.443
4097 time:55.8449
Time consuming varies with the width of the image is here
It is clear that only when the image width is 1024,2048,4096 the execute time is abnormal, while the overall trend is increasing linearly.
Could you try using this version to see if the same difference exist
Also added test to see if loaded image is of type CV_8UC1.
Be aware that the result of your and my solution is a mirrored image
int lWidth4096 = image4096.size().width;
int lHeight4096 = image4096.size().height;
Mat image4096Res = cv::Mat::zeros(lWidth4096, lHeight4096, CV_8UC1);
int lWidth4097 = image4097.size().width;
int lHeight4097 = image4097.size().height;
Mat image4097Res = cv::Mat::zeros(lWidth4097, lHeight4097, CV_8UC1);
if (image4096.type() != CV_8UC1)
throw "need 8bit image as input";
if (image4097.type() != CV_8UC1)
throw "need 8bit image as input";
for (int i = 0; i < 10; i++)
{
double time0 = static_cast<double>(getTickCount());
for (int j = 0; j < lWidth4096; ++j)
{
uint8_t *buf = image4096Res.ptr(j);
for (int i = 0; i<lHeight4096; ++i)
{
//image4096Res.at<uchar>(j, i) = image4096.at<uchar>(i, j);
//buf[i] = image4096.at<uchar>(i, j);
buf[i] = (image4096.data + image4096.step[0] * i)[j];
}
}
double time4096 = (static_cast<double>(getTickCount()) - time0) / getTickFrequency();
double time1 = static_cast<double>(getTickCount());
for (int j = 0; j < lWidth4097; ++j)
{
uint8_t *buf = image4097Res.ptr(j);
for (int i = 0; i<lHeight4097; ++i)
{
//image4097Res.at<uchar>(j, i) = image4097.at<uchar>(i, j);
//buf[i] = image4097.at<uchar>(i, j);
buf[i] = (image4097.data + image4097.step[0] * i)[j];
}
}
double time4097 = (static_cast<double>(getTickCount()) - time1) / getTickFrequency();
std::cout << "4096 time:" << time4096 * 1000 << std::endl;
std::cout << "4097 time:" << time4097 * 1000 << std::endl;
std::cout << std::endl;
}

when I try to pop from a queue it doesn't pop why?

I have the following code but the pop() function doesn't work I don't
know why.
#include < iostream >
#include < fstream >
#include < sstream >
#include < string >
#include < queue >
using namespace std;
int quantum;
struct process {
string name;
int arrival;
int burst;
int priority;
};
process parseProcess(string s)
//parse the given string s to find the process information
{
process p;
stringstream lineStream;
lineStream.str(s);
string item;
getline(lineStream, item, '$');
p.name = item;
getline(lineStream, item, '$');
p.arrival = stoi(item);
getline(lineStream, item, '$');
p.burst = stoi(item);
getline(lineStream, item, '$');
p.priority = stoi(item);
cout <<
p.name << "\t" << p.arrival << "\t" << p.burst << "\t" << p.priority << "\t" << endl;
return p;
}
void second_write(queue < process > s, int n, int quantum) {
int * burst_Time = new int[n], total = 0,
total_turn_arround = 0;
float avgwaiting_time = 0, avgturn_arround =
0;
int * remaining_burst_time = new int[n];
int * waitting_time = new
int[n];
int * turn_arround_time = new int[n]; //now queue doesn't pop
Why ? ? ? ? ? process temp = s.front();
process p;
queue < process > qtemp;
for (int i = 0; i < n; i++) {
p.name = temp.name;
p.arrival = temp.arrival;
p.burst = temp.burst;
burst_Time[i] = p.burst;
p.priority = temp.priority; * * s.pop(); * * qtemp.push(p);
cout
<< temp.name << "\t" << temp.arrival << "\t" << temp.burst << "\t" <<
temp.priority << endl;
}
for (int i = 0; i < n; i++) {
remaining_burst_time[i] = burst_Time[i];
}
while (1) {
bool finished = true;
for (int i = 0; i < n; i++) {
if (remaining_burst_time[i] > 0) {
finished = false;
if (remaining_burst_time[i] > quantum) {
total += quantum;
remaining_burst_time[i] -= quantum;
} else {
total += remaining_burst_time[i];
waitting_time[i] = total - burst_Time[i];
remaining_burst_time[i] = 0;
}
}
}
if (finished == true) break;
}
for (int i = 0; i < n; i++) {
turn_arround_time[i] = burst_Time[i] - waitting_time[i];
}
for (int i = 0; i < n; i++) {
total += waitting_time[i];
total_turn_arround += turn_arround_time[i];
}
avgturn_arround =
total_turn_arround / n;
avgwaiting_time = total / n;
int w = 0;
while (!qtemp.empty()) {
process temp2 = qtemp.front();
qtemp.pop();
}
}
int main() //it takes two arguments 1:inputFile name,
2: outputFile name {
string fileName;
cout << "enter file name " <<
endl;
cin >> fileName;
cout << "Enter quantum";
cin >> quantum;
ifstream infile(fileName);
string line;
queue < process > q;
while (getline(infile, line)) {
process p = parseProcess(line);
q.push(p);
}
int Length = q.size();
second_write(q, Length, quantum);
/*Your code must be written here in order to sort the processes in
the queue in terms of the chosen cpu scheduling algorithm. Your code
also needs to calcualte average wating time and average turnarround
time. Finally your code needs to print the Gantt chart, waiting time
for each process and the average waiting time and the average
turnaround time on the screen */
return 0;
}

Resources