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]);
Related
The Function won't initiate can someone help? When I run it in the debugger program skips over function and I don't know why?
#include <iostream>
using namespace std;
int size_array= 0;
int *data_array;
void sorting(int *[], int);
int main()
{
cout<<"enter in array size \n";
cin>>size_array;
int *data_array=new int(size_array);
for(int i=0;i<size_array;i++)
{
cout<<"enter number "<<i+1<<endl;
cin>>data_array[i];
}
**int sorting(int data_array, int size_array);**
for (int i=0; i<size_array;i++)
{
cout<<data_array[i]<<endl;
}
return 0;
}
The marked code is simply declaring the function, not calling it.
Also, your data_array is a pointer to a single int whose value is initialized as size_array. But you want an array of size_array number of ints instead.
Try this:
#include <iostream>
using namespace std;
void sorting(int[], int);
int main()
{
int size_array = 0;
cout << "enter in array size \n";
cin >> size_array;
int *data_array = new int[size_array];
for(int i = 0; i < size_array; i++)
{
cout << "enter number " << i+1 << endl;
cin >> data_array[i];
}
sorting(data_array, size_array);
for (int i = 0; i < size_array; i++)
{
cout << data_array[i] << endl;
}
delete[] data_array;
return 0;
}
void sorting(int data_array[], int size_array)
{
// sort data_array as needed...
}
Need some help with the problem.
When I compile my code below, it gives me this error:
error: invalid conversion from " int* " to " int "
I want to create a calculatePercentage function, so I can use the value when I call it.
void calculatePercentage(int voteResult[],int percentage[])
const int NO_OF_CANDIDATE = 10;
int main()
{
ifstream input("votes.txt",ios::in);
string candidates[NUMBER_OF_CANDIDATE];
int voteResult[NUMBER_OF_CANDIDATE];
int percentage[NUMBER_OF_CANDIDATE];
for (int i = 0; i < NUMBER_OF_CANDIDATE; i++) {
input >> candidates[i] >> voteResult[i];
}
calculatePercentage(voteResult, percentage); // error happened here.
return 0;
}
void calculatePercentage(int voteResult[],int percentage[])
{
int totalVotes = 0;
for (int i = 0; i < NUMBER_OF_CANDIDATE; i++)
{
totalVotes += votes[i];
}
for (int j = 0; j < NUMBER_OF_CANDIDATE; j++)
{
double wk_percentage = static_cast<double>{votes[j])/totalVotes;
percentage[j]=static_cast<int>(wk_percentage*100);
}
}
The code you posted has a bunch of errors.
calculatePercentage() definition is missing the closing ';'
probable name mismatch between NO_OF_CANDIDATE and NUMBER_OF_CANDIDATE
missing #include <fstream> (using ifstream)
missing #include <string>
missing std:: namespace before ifstream and string
votes not declared anywhere (should it be voteResult?)
wrong opening curly bracket instead in static_cast<double>{votes[j])/totalVotes;
...
Let alone the way you do calculation and parameter passing...
The following edited code should compile, not sure if it works as you expected:
#include <fstream>
#include <string>
void calculatePercentage(int voteResult[], int percentage[]);
const int NUMBER_OF_CANDIDATE = 10;
int main()
{
std::ifstream input("votes.txt");
std::string candidates[NUMBER_OF_CANDIDATE];
int voteResult[NUMBER_OF_CANDIDATE];
int percentage[NUMBER_OF_CANDIDATE];
for (int i = 0; i < NUMBER_OF_CANDIDATE; i++) {
input >> candidates[i] >> voteResult[i];
}
calculatePercentage(voteResult, percentage); // error happened here.
return 0;
}
void calculatePercentage(int voteResult[], int percentage[])
{
int totalVotes = 0;
for (int i = 0; i < NUMBER_OF_CANDIDATE; i++)
{
totalVotes += voteResult[i];
}
for (int j = 0; j < NUMBER_OF_CANDIDATE; j++)
{
double wk_percentage = static_cast<double>(voteResult[j]) / totalVotes;
percentage[j] = static_cast<int>(wk_percentage*100);
}
}
Also on Ideone.
Thanks for everyone's comment. I skipped some statements and made some typos when posting them. I fixed them as follows. The only error occurred is that still cannot convert " int* " from " int". Just don't know how to solve it.
#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
void calculatePercentage(int voteResult[],int percentage[]);
const int NUMBER_OF_CANDIDATE = 10;
int main()
{
ifstream input("votes.txt",ios::in);
string candidates[NUMBER_OF_CANDIDATE];
int vote[NUMBER_OF_CANDIDATE];
int percentage[NUMBER_OF_CANDIDATE];
for (int i = 0; i < NUMBER_OF_CANDIDATE; i++) {
input >> candidates[i] >> vote[i];
}
calculatePercentage(voteResult, percentage); // error happened here.
return 0;
}
void calculatePercentage(int voteResult[],int percentage[])
{
int totalVotes = 0;
for (int i = 0; i < NUMBER_OF_CANDIDATE; i++)
{
totalVotes += votes[i];
}
for (int j = 0; j < NUMBER_OF_CANDIDATE; j++)
{
double wk_percentage = static_cast<double>(votes[j])/totalVotes;
percentage[j]=static_cast<int>(wk_percentage*100);
}
}
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.
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