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
Related
I want:
*!!
**!!!!
***!!!!!!
// And so on.
My attempt is below:
#include <iostream>
using namespace std;
int main()
{
int a;
int ex;
for (a = 1; a <= 5; a++)
{
cout<<"*";
for(ex =1; ex<= 2*a; ex++)
{
cout<<"!";
}
cout<<endl;
}
return 0;
}
I get this as the output:
*!!
*!!!!
*!!!!!!
//and so on...
It does what I need it to do for the second symbol but I don't know how to arrange the loops so that first symbol is outputted the desired number of times and not cut off by the second loop.
there is a small logical mistake in your code, you are only printing '*' once every loop. use the code below
#include <iostream>
using namespace std;
int main()
{
int a;
int ex;
for (a = 1; a <= 5; a++)
{
cout<<std::string((a),'*');
cout<<std::string((a*2),'!');
cout<<endl;
}
return 0;
}
You need to have the cout << '*' statement in a loop as well:
int main()
{
int a;
int ex;
for (a = 1; a <= 5; a++) // signifies the number of lines to print
{
auto i = 1;
while (i <= a) // prints * a times
{
cout<<"*";
++i;
}
for(ex =1; ex<= 2*a; ex++) // prints ! 2*a times
{
cout<<"!";
}
cout<<endl;
}
return 0;
}
You need another loop to print a-counted * symbols inside the main loop.
#include <iostream>
using namespace std;
int main()
{
int a;
int ex;
for (a = 1; a <= 5; a++)
{
for(int i = 0; i < a; ++i)
{
cout<<"*";
}
for(ex =1; ex<= 2*a; ex++)
{
cout<<"!";
}
cout<<endl;
}
return 0;
}
Another solution is:
#include <iostream>
using namespace std;
int main(){
int times = 5;
char simbol1 = '*', simbol2 = '!';
for(int i=1 ; i<=times ; i++){
for(int k=0; k<i; k++) cout << simbol1;
for(int j=0; j<i*2; j++) cout << simbol2;
cout << endl;
}
return 0;
}
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.
I have created a function that returns the sum of all the numbers in an array, however I keep getting an error message: error: invalid conversion from 'int (*)[6]' to 'int' [-fpermissive]. In addition I also get an error that says: error: initializing argument 1 of 'int getTotal(int)'[-fpermissive]. It seems like these two errors go together. Am I supposed to use pointers? I have spent hours trying to figure this out with no luck.
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
const int ROWS = 4;
const int COLS = 6;
void openInputFile(ifstream &,string);
int getTotal(int);
using namespace std;
int main()
{
int tot; //total of all numbers
int val;
int twoArray[ROWS][COLS];
ifstream inFile;
string inFileName = "nums.txt";
//Opening file
openInputFile(inFile, inFileName);
//Create 2D array
for(int i=0; i<ROWS; i++)
{
for(int j=0; j<COLS; j++)
{
inFile >> twoArray[i][j];
}
}
//Close
inFile.close();
//THIS IS WHERE ERROR IS
tot = getTotal(twoArray);
printArray(twoArray);
return 0;
}
void openInputFile(ifstream &inFile, string theFile)
{
inFile.open(theFile.c_str());
if(!inFile)
{
cout << "Error opening the file!\n";
exit(13);
}
}
int getTotal(int array[][COLS])
{
int sum = 0;
for(int i=0; i<ROWS; i++)
{
for(int j=0; j<COLS; j++)
{
sum+=array[i][j];
}
}
return sum;
}
int printArray(int array[][COLS])
{
for(int i=0; i<ROWS; i++)
{
for(int j=0; j<COLS; j++)
{
cout << array[i][j] << " ";
}
cout << endl;
}
return 0;
}
The problem is in wrong function declaration. You have at the beginning of code:
int getTotal(int);
and then define function as:
int getTotal(int array[][COLS])
So, edit your functions' declaration. By the way I cannot see declaration for:
int printArray(int array[][COLS]);
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;
}
I've got this code:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<string> *vecptr;
int veclen;
void getinput()
{
string temp;
for(int i = 0; i < 3; i++)
{
cin>>temp;
vecptr->push_back(temp);
}
veclen = vecptr->size();
}
int main()
{
getinput();
for(int i = 0; i < veclen; i++)
{
cout<<vecptr[i]<<endl;
}
return 0;
}
My compiler(G++) throw me some errors: test2.cpp:28:17: error: no match for 'operator<<' in 'std::cout << *(vecptr + ((unsigned int)(((unsigned int)i) * 12u)))' ...
What's wrong? What can I do to fix it?
The program is still not completely right. You have to initialize the vector pointer and then give it a size and the use it. A full working code could be,
#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<string> *vecptr = new vector<string>(10);
int veclen;
void getinput()
{
string temp;
for(int i = 0; i < 3; i++)
{
cin>>temp;
(*vecptr)[i] = temp;
}
veclen = (*vecptr).size();
}
int main()
{
getinput();
for(int i = 0; i < veclen; i++)
{
cout<<(*vecptr)[i]<<endl;
}
return 0;
}
Although I have mentioned the size as 10 you could make it a variant.
You need to dereference vecptr here to get the underlying vector:
cout << (*vecptr)[i] << endl;
You will also need to initialize vecptr.