Expected unqualified-id error in c++ for loop - c++

Please help this error has been driving me crazy and I have no idea how to fix it. The error occurs on the line indicated by the asterisks. In addition, throughout the entirety of that for loop it gives syntax errors. Any help would be greatly appreciated.
#include <iostream>
#include "Matrix.h"
#include <vector>
using namespace std;
Matrix::Matrix(int rownum, int columnnum, bool israndom)
{
this->rownum= rownum;
this->columnnum= columnnum;
}
// Makes random number from 0-1
double Matrix::generaterand()
{
double r= rand()/RAND_MAX;
return r;
}
****** for(int i=0; i<rownum; i++) ********
{
vector<double> columnvalues;
for(int j=0; j<columnnum; j++)
{
double r = 0.00;
if(isRandom)
{
r= generaterand();
}
columnvalues.push_back(r);
}
this->values.push_back(columnvalues);
}
void Matrix::printtoconsole()
{
for(int i=0; i<rownum; i++)
{
for(int j=0; j<columnnum; j++)
{
cout << this->values.at(i).at(j) << "\t\t";
}
cout << endl;
}
}

Your for loop is not embedded in a function. It's just sitting alone while everything else you have is in its own functions. You should put it in its own function. That might help.

Related

Why does this code timeout on using break but not when I exit using loop counter variable?

I saw some algorithm for a problem and rewrote it but I changed the fast_count_segment function's inner loop exit condition j=n1 to the break statement,the code timed out. Why does this happen?
The code runs fine otherwise but when I submit to the online grader, it shows code timeout, but as soon as I replace break with j=n1, it passes.
Shouldn't both be doing the same thing?
Code:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> fast_count_segments(vector<pair<int,int>> &v, vector<int> points) {
vector<int> cnt(points.size());
int n1=v.size(),n2=points.size(),i,j,count;
for(i=0;i<n2;i++){
count=0;
for(j=0;j<n1;j++){
if(points[i]>=v[j].first && points[i]<=v[j].second)
count++;
else if(points[i]<v[j].first)
break; //j=n1; Changed this statement to break
}
cnt[i]=count;
}
return cnt;
}
int main() {
int n, m,a,b;
cin >> n >> m;
vector<pair<int,int>> v;
for (size_t i = 0; i < n; i++) {
cin >>a>>b;
v.push_back(make_pair(a,b));
}
vector<int> points(m);
for (size_t i = 0; i < points.size(); i++) {
cin >> points[i];
}
sort(v.begin(),v.end());
vector<int> cnt = fast_count_segments(v, points);
for (size_t i = 0; i < cnt.size(); i++) {
cout << cnt[i] << ' ';
}
cout<<endl;
return 0;
}

main.exe has stopped working in code blocks

This is a code I wrote for bubble sort. I gave a comment //this line due to which I'm unable to run this program. Every time the first element of the array needs to be stored in 'temp'.
#include <bits/stdc++.h>
using namespace std;
int main()
{
int arr[7]={7,8,5,2,4,6};
int temp;
for(int i=0;i<7;i++)
{
temp=arr[0]; //this line.
for(int j=0;j<7-i;j++)
{
if(temp<arr[j])
temp=arr[j];
else
swap(arr[j],arr[j-i]);
}
}
for(int k=0;k<7;k++)
{
cout<<arr[k]<<endl;
}
return 0;
}
There were some issue with your program:
Array size should be 6 instead of 7
The for loop condition was incorrect
swap(arr[j],arr[j-i]) will break when j-i is less than 0(for instance i=1, j=0).
Program
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int arr[6]={7,8,5,2,4,6};
for(int i=0;i<5;i++)
{
for(int j=0;j<5-i;j++)
{
if(arr[j]>arr[j+1])
swap(arr[j],arr[j+1]);
}
}
for(int k=0;k<6;k++)
cout<<arr[k]<<endl;
return 0;
}
Ideone
You seem flipped for() loops over... what I got - not the most elegant solution, but I stick to the same tools you're using. Mostly. I could make it as template and it would work with any appropriate container. std::sort sometimes implemented like that.
#include <iostream>
#include <algorithm>
void bubbleSort(int arr[], int n)
{
bool swapped;
for (int i = 0; i < n-1; i++)
{
swapped = false;
for (int j = 0; j < n-i-1; j++)
{
if (arr[j] > arr[j+1])
{
std::swap(arr[j], arr[j+1]);
swapped = true;
}
}
// no elements were swapped, array already sorted.
if (!swapped) break;
}
}
int main()
{
int arr[] = {7,8,5,2,4,6};
bubbleSort(arr, std::size(arr));
for( auto v : arr )
std::cout << v << " ";
std::cout << std::endl;
}
In C++11 and later <algorithm> can be replaced by <utility>, it's just for swap/size.

Run-Time Check Failure #2 - Stack around the variable 'A' was corrupted. Solution needed

I started to learn C++ and encounter a error.
I just create a class that produce a 10x10 matrix, put integers 0 to 99 into the matrix and print them out. However, it pop out a stack error with one of the value that supposed to be 93 become 0. I have no clue about it. Thank you for your answer.
//myclass.h
#pragma once
class Matrix {
private:
int _2dmatrix [10][10];
public:
Matrix ();
~Matrix ();
void printMatrix ();
};
// myclass.cpp
#include "myclass.h"
#include <iostream>
using namespace std;
Matrix::Matrix () {
for (int i = 0; i < 10; i++)
for (int j = 0; j < 10; j++) {
_2dmatrix[i+1][j+1] =i*10+j;
}
}
Matrix::~Matrix () {}
void Matrix::printMatrix () {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
cout << _2dmatrix[i+1][j+1] << "\t";
}
cout << "\n";
}
}
//source.cpp
#include <iostream>
#include "myclass.h"
using namespace std;
int main() {
Matrix A;
A.printMatrix();
return 0;
}
Arrays in C++ are zero-based. Simply change i+1to i and j+1 to j.

Printing non 0s and position from matrix in C++?

Hi I have this code written to read in a matrix with the dimension given. However I want to modify it to read in a matrix from a file from the command line using cin. I then want it to print row by row the position of a non zero element followed by that value. Can someone help me modify this. Thanks.
#include <fstream>
#include <iostream>
#include <stdlib.h>
//using std:#include <fstream>
//#include <iostream>
using std::cin;
using std::cout;
using std::cerr;
using std::endl;
using std::ofstream;
void readArray(int, int, double **);
int nz=0;
main(int argc, char *argv[])
{
int rowCT = atoi(argv[1]);
int colCT = atoi(argv[2]);
// here you reserve the space for the array
double **A = new double*[rowCT];
for (int i = 0; i < rowCT; i++)
A[i] = new double[colCT];
readArray(rowCT, colCT, A);
}
//int count = new int[rowCT];
void readArray(int r, int c, double **arr)
{
//here r rows of c elements are read in
int count[r];
int total=0;
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
cin>> arr[i][j];
if(arr[i][j]>0)
{
nz++;
}
}
count[i]=nz;
nz = 0;
}
cout<< r << endl;
for(int i=0; i<r; i++)
{
cout<<count[i]<< " ";
for(int j=0; j<c; j++)
{
if(arr[i][j]>0)
{
cout<< j+1 << " " << arr[i][j] << " ";}
}
cout<< endl;
}
for(int i =0; i<r; i++)
{
total+= count[i];
}
cout << total<< endl;
}

How to manipulate double array data in C++?

I am getting error C2297: '*' : illegal, right operand has type 'double *' in this piece of code:
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
double cx=0.5;
double**image_array;
image_array= new double*[5];
for (int i=0;i<5;i++)
{
image_array[i]= new double[5];
for(int j=0;j<5;j++)
{
image_array[i][j]=0;
}
}
for (int i=0;i<5;i++){
for(int j=0;j<5;j++)
{
int i=cx*image_array[i,j];
}
}
system("PAUSE");
return 0;
}
Can anyone tell the reason. Can't I multiply the double array with double type data? Or what else can I do?
image_array[i,j] doesn't do what you want. You need image_array[i][j] instead.
There's a few problems with your code - cleaning up the formatting, the main problem is your attempt to index image_array[i,j]. Use image_array[i][j] - here's a working interpretation of your code fragment, with some modifications.
#include <iostream>
using namespace std;
int main()
{
double cx=0.5;
double**image_array;
image_array= new double*[5];
for (int i=0;i<5;i++) {
image_array[i]= new double[5];
for(int j=0;j<5;j++)
{
image_array[i][j]=i*5+j;
}
}
for (int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
double d=cx*image_array[i][j];
cout << d << "-";
}
cout << endl;
}
//system("PAUSE");
return 0;
}
You can see sample output here: http://codepad.org/38r4CZ9W