Compiler error: invalid conversion from 'int' to 'int*' [-fpermissive] - c++

I am having a problem with a program, and I'm getting the error above. However, when I search up the error, everyone else has some sort of int * variable whereas I don't have that all compared to them and is still getting this error.
#include <iostream>
#include <fstream>
const int VALUES = 250;
using namespace std;
void minFinder(int nums[]);
void maxFinder(int nums[]);
void arithmeticMeanFinder(int nums[]);
void geometricMeanFinder(int nums[]);
void standardDeviationFinder(int nums[]);
int main()
{
ifstream file;
int number, counter;
int nums [VALUES];
counter = 0;
file.open("F://Yes/Untitled.txt");
file >> number;
while (!file.fail()){
counter++;
nums [counter-1] = number;
file >> number;}
arithmeticMeanFinder(nums[VALUES]);
file.close();
system("pause");
return 0;
}
void arithmeticMeanFinder (int nums[VALUES])
{
ifstream file;
int ct, holder;
double counter, mean;
double accum = 0;
for (ct = 0; ct < VALUES; ct++){
holder = nums[ct];
accum = accum + holder;
counter++;}
mean = (accum * 1.0) / counter;
cout << counter << " is the arithmetic mean" << endl;
}

This code: arithmeticMeanFinder(nums[VALUES]); index into nums to retrieve the (nonexistent) item at the offset VALUES.
I'd guess you want it to be more like: arithmeticMeanFinder(nums);
The rest of the code isn't exactly what I'd like (e.g., it requires that the number of values in the file be exactly equal to VALUES, or it'll fail miserably), but that's the source of the specific problem the compiler is citing.

In your code, you have:
void arithmeticMeanFinder (int nums[VALUES])
Because of the rules of C++, this is equivalent to:
void arithmeticMeanFinder (int nums[])
And also because of the rules of C++, this is equivalent to:
void arithmeticMeanFinder (int *nums)
So when you call this function, you need to write:
arithmeticMeanFinder(nums); // pass the pointer to first element
Instead of:
arithmeticMeanFinder(nums[VALUES]);
In the call to arithmeticMeanFinder above, by expression nums[VALUES], you pass the VALUES-th element, which is an int, as the argument.
As the array nums has only VALUES items (maximum index is VALUE - 1),
this is an out-of-bound access.

Related

How to scan an integer array

I am new to C++. I am trying to define a binary converter function and return a pointer. Then U want to display generated binary in the main function:
#include <iostream>
using namespace std;
int* binary_con(int n)
{
int i;
int binary[100];
for (i = 0; n > 0; i++)
{
binary[i] = n % 2;
n = n / 2;
}
return binary;
}
int main()
{
int n;
int* a;
cout << "Input the number:";
cin >> n;
a = binary_con(n);
while (*a)
{
cout << *a;
a++;
}
return 0;
}
But after I run my code, I got this:
Can anyone explain this to me?
you can't return an array from a function, the way is to pass that array as an argument to the functionm using this approach:
void binary_con(int n, int *binary)
now you have access to binary array inside your function, hence you can edit it and see the changes outside of the function without returning anything.
inside your main, instead of writing a = binary_con(n);, you should write this:
binary_con(n, a);

Need help passing an array of structure to a function

So, I am currently studying at school and I need to do my homework. I am a beginner in C++ and somehow the compiler shows me an error in my code. Basically, I have a .txt file where the data is stored.
The .txt file looks something like this:
5
Petras 23.25 10.50
Rimas 125.40 1.20
Romas 55.00 1.00
Jurgis 1000.90 0.25
Algis 15.00 25.50
The first line shows how much people in the list we have, so I created integer n.
Next we have a list of people. The list tells the name of the person, how much money he has in different currency, and shows the rate of exchange to euros.
And the problem is that I am trying to find the sum of the money they have in Euros. This is my code.
#define USE_MATH_DEFINES
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>
using namespace std;
const int Cn = 100;
const int Cname = 15;
int n;
struct listofpeople {
string name;
double MoneyInOtherCurrency;
double RateOfExchange;
double MoneyInEuros;
};
listofpeople A[Cn + 1];
void data();
void ChangeCurrency();
double sum(double C[], int m);
int main () {
data();
ChangeCurrency();
cout << sum(A[].MoneyInEuros, n);
return 0;
}
//-------------------------------------------------------
void data(){
ifstream is ("U2duom.txt");
is >> n;
char symbols[Cname + 1];
for(int i = 1; i <= n; i++){
is.ignore(80, '\n');
is.get(symbols, Cname);
A[i].name = symbols;
is >> A[i].MoneyInOtherCurrency;
is >> A[i].RateOfExchange;
}
}
//----------------------------------------------------------
void ChangeCurrency(){
for(int i = 1; i <= n; i++){
A[i].MoneyInEuros = A[i].MoneyInOtherCurrency*A[i].RateOfExchange;
cout << A[i].name << " " << A[i].MoneyInEuros << " " <<
A[i].MoneyInOtherCurrency << " " << A[i].RateOfExchange << endl;
}
}
//---------------------------------------------------------------
double sum(double C[], int m){
double a = 0;
for(int i = 1; i <= m; i++){
a= a + C[i];
}
return a;
}
And the problem is that the compiler shows me an error in the line where i try to print the sum. Could anyone help me? Thanks.
EDIT:
My compiler shows this error:
error: expected primary-expression before ']' token
If I specify array elements I want to use, for example:
sum(A[n].MoneyInEuros, n);
The compiler shows this error:
cannot convert 'double' to 'double*' for argument '1' to 'double
sum(double*, int)'
double sum(double C[], int m); takes an array of doubles, but you only have an array of listofpeople. This doesn't work.
You have to change sum so that it takes an array of listofpeople, or you change your data structure from a Structure Of Arrays to an Array of Structures (better for performance, more complex to handle).
Typically, sum requires reimplementing:
double sum(listofpeople *s, int m);
Be aware that in C++, we don't use the [] notation for types.

Hi, Just Want to Know What This Error Means

"error C2660: 'storeInitialValues' : function does not take 1 arguments" shows up in the log of my code when I try to build. I've looked at some past errors posted here and I think it might be some kind of initialization error with either/all the usersize, v, dsize, and/or asize. I just want to see the error on the specific calling of storeInitialValues(usersize, v, dsize, asize); that's it. Thank you very much in advance.
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <ctime>
#include <cstdlib>
using namespace std;
struct vec
{
};
struct arr
{
};
void fillArray(int A[], int size);
void storeInitialValues(int * & arr, int & asize, int & dsize, vector<int>& v, int & usersize);
int main()
{
int usersize, dsize, asize;
vector <int> v;
int * ptr = new int[10];
cout << "How many values in data structures? Please enter values greater than 20." << endl;
cin >> usersize;
while (usersize < 21)
{
cout << "Error, enter values greater than 20!" << endl;
cin >> usersize;
}
cout << "Alright, here are your numbers: " << endl;
storeInitialValues(usersize, v, dsize, asize);
}
// fillArray stores sequential, unique, integer values into an array and
// then randomizes their order
void fillArray(int A[], int size)
{
srand((int)time(0));
for (int i = 0; i < size; i++)
{
A[i] = i + 1;
}
for (int k = size - 1; k>1; k--)
{
swap(A[k], A[rand() % k]);
}
}
// storeInitialValues calls fillArray to produce an array of unique randomly
// organized values and then inserts those values into a dynamically sized
// array and a vector.
void storeInitialValues(int * & arr, int & asize, int & dsize, vector<int>& v, int usersize)
{
int * temp = new int[usersize]; // temporary array for randomized data
fillArray(temp, usersize); // get data
for (int i = 0; i < usersize; i++) // copy data into the dynamic data structures
{
add(arr, asize, dsize, temp[i]);
v.push_back(temp[i]);
}
delete[] temp; // clean up temporary pointer
temp = NULL;
}
void add(int & usersize, int & arr, int & dsize, int & temp[i])
{
}
void remove()
{
}
Nothing about your call to storeInitialValues matches the declaration. I think you might be confused thinking the names of the variables are important. That's not the case. You have to pass variables that match the type of the variables in the function declaration in the correct order, the name are irrelevant.
int * & arr is a very strange declaration. int *arr would be a pointer to an int that you could treat as an array. What exactly are you aiming for with int * &? Mixing * and & requires that you be very careful with your usage. But you are also using vector, which is a very safe way of dealing with arrays. Why not just use vectors? You also declare and allocate ptr in the main function but you don't use it nor do you delete it.

To write a program to remove duplicates from an array

I am working on program that remove duplicates from an array I am using three functions here: one to take the input such as the size and the number, second function to remove duplicates and return the number without duplicates and the third function just a report show the size and the new number but I am having problem I don't know in what step I think in report or phillip erreur:
In function ‘int main()’: invalid conversion from ‘int*’ to ‘int’,initializing argument 1 of ‘void report(int, int)’
#include <iostream>
using namespace std;
const int size = 100;
void phillip(int[], int & );
/* Preconditions: Array of base type in declared and int varuable declared
postconditions: the array is filled with values supllied by the user at
the keybord. the user is assked how many values they want - this value is
given to the second argument.
*/
int remdubs(int[], int noel);
/* Preconditions: An array of basetype int that has noel values.
postconditions: The number of unique elemts in the array is returned. The function removes all dubplicates inside the array.
*/
void report(int s, int d);
int main()
{
int ruby[size];
int numele, numuniq;
phillip(ruby, numele);
numuniq = remdubs(ruby, numele);
report(ruby, numuniq);
return 0;
}
void phillip(int[], int& )
{
int s;
cout << "\nHow many values you want? ";
cin >> s;
cout << "\nPlease input 10 integers, hitting return after each one \n";
for (int i = 0; i < s; i++)
{
int num;
cin >> num;
}
}
int rembups(int sapphire[], int noel)
{
for (int i = 0; i < noel; i++)
{
for (int j = i + 1; j < noel; j++)
{
if (sapphire[i] == sapphire[j])
{
for (int k = j; k < noel; k++)
sapphire[k] = sapphire[k + 1];
noel--;
j--;
}
}
}
return noel;
}
void report(int s, int d)
{
cout << "\nYou entered " << s << "distinct numbers: " << d;
}
Can't explain it better than your error:
void report (int s, int d);
this function asks for two integer values, you're passing an array to it which, with decayed functionalities, will behave like an integer pointer
int ruby[size];
report (ruby, numuniq);
I'm unsure on the behavior of your program but you should rather do something like
report(ruby[0], numuniq);
that is: access the element of the array and feed it to the function
The error is in the function report, it asks for two integer and you are passing an array and an integer instead
and again i don't think you need 2 parameters in the function report one would solve your purpose
void report(int d)
{
cout << "\nYou entered <<d<<" distinct numbers";
}
that would solve your error but i don't think you would get your desired output
Few problems with the code:
Report function should be expecting an array of integers rather than single integer
Try using human readable variable or function names
remdubs was renamed to rempubs in function definition
phillip function definition didn't have params defined properly
You have const int size but do not use it
remdups has a bug somewhere. I will leave that out as I am trying to fix other issues here.
There are better ways of finding duplicates than remdups. Please look at some other solutions.
I have fixed your code and provided it here on ideone

c++ two errors while compiling

I'm a beginner in c++ and I'm getting two errors in my code and I don't know how to fix them...
the first one
illegal indirection
and the second one is
'=' left operand must be a I-value. (in the line: ((ArrayPtr +i)+j)=rand()%55+1 )
Does anyone have an idea how to fix them? That's my code:
#include <iostream>
#include <math.h>
#include <time.h>
#include<iomanip>
#include<array>
#include <algorithm>
using namespace std;
const int AS = 6;
void FillingRandomly(int (*)[AS]);
void printing(int (*)[AS]);
int c;
int main()
{
int funny = 0;
int timpa = 0;
int counter = 0;
int Array[AS][AS];
srand(time(0));
FillingRandomly(Array);
cout << "The unsorted array is" << endl << endl;
printing(Array);
cout << "The sorted array is" << endl << endl;
printing(Array);
system("PAUSE");
return 0;
}
void FillingRandomly(int *ArrayPtr)
{
for(int i=0;i<AS;i++)
{
for (int j=0;j<AS;j++)
{
*(*(ArrayPtr +i)+j)=rand()%55+1;
}
}
}
void printing(int *Array)
{
for(int i=0;i<AS;i++)
{
for (int j=0;j<AS*AS;j++)
{
int counter = 0;
cout<<((Array[i] +j))<<setw(5);
if ((Array[i] +j)%AS == 0)
cout << endl << endl;
}
}
}
void forsorting(int *Brray, int funny)
{
int dice = 0;
int super = 0;
int space=0;
//Sorting Array[][] which is treated like Array[]
{
for (int pass = 0; pass < AS - 1; pass++) {
for (int k = 0; k < AS - 1; k++) {
int temp;
if(*(Brray+k)==*(Brray+k+1))
{
temp=*(Brray+k);
*(Brray+k)=*(Brray+k+1);
*(Brray+k+1)=temp;
}
}
}
}
}
By
*(*(ArrayPtr +i)+j)=rand()%55+1;
it seems you want
ArrayPtr[i][j] = (rand() % 55) + 1;
You can try something along the line of
int const offset = AS * i + j;
int const elem = (rand() % 55) + 1;
*(ArrayPtr + offset) = elem;
Your function signature is:
void FillingRandomly(int *ArrayPtr)
where you are telling to compiler that you are passing a simple pointer, but in the line:
*(*(ArrayPtr +i)+j)=rand()%55+1;
you are doing a double derreference, which is illegal and causing the compiler to complain
COMPLEMENT
I was seeing the comments in the other answer and, as what I need to write is bigger than the reserved commentary space, I decided to complement my own answer.
You defined Array as:
int Array[AS][AS];
Indeed, what you are doing is a promise to compiler that you will use Array as defined, but the compiler doesn't believe in you too much, so that any time you use Array the compiler will make sure that it is being used as declared.
The problem arises when you declare your FillingRandomly function. Here you are broking your promise and are trying to use Array by declaring a differente type. Note how you declare your function:
void FillingRandomly(int *ArrayPtr)
Due the fact that c++ supports function overloading, the compiler doesn't warn you until it initiate the linking phase, when it is unable to find a function whose signature is:
void FillingRandomly(int ArrayPtr[][AS])
note that both are different.
Once you are a beginner, the best way to keep your programs correctly is to keep your promise immutable. Bellow I show you a piece of your own code, correcting those issues for FillingRandomly function (you have to correct it for the others functions too):
const int AS = 6;
void FillingRandomly(int [][AS]); // Note that I've changed your prototype here
....
void FillingRandomly(int ArrayPtr[][AS]) // Keep your function signature the same as your prototype signature
{
for(int i=0;i<AS;i++)
{
for (int j=0;j<AS;j++)
{
ArrayPtr[i][j]=rand()%55+1; // Note how ArrayPtr is being used exactly as your promised early
}
}
}