Hello. I get an error in lines 5 and 33. I have forgotten much about functions, errors say :
1.'i' was not declared in this scope
2. expected ')' before ',' token
3. expected unqualified-id before 'int'
My function should find the highest number in an array. I am using my function in line 15
That's just not how to declare a function. Each parameter must specify the type of the parameter, and may specify the name of the parameter.
If the first parameter is meant to be an int array, named p:
void funkcija(int p[], int k, int i, int & k);
While I'm here, it's pretty safe to say that you should use an array when you have to and a vector (or std::array) in all other situations. In this case, use a vector.
1 2 3
for (int i = 0; i < n ; i++)
fd >> p[i];
funkcija(p[i],k, i, k);
Are both those lines meant to be inside the for loop? They're not. Only the first one is. Use braces for every for loop, always.If you don't, one day you'll get it wrong or someone else who has to maintain or understand your code will.
1 2 3 4 5 6
void funkcija(int p[i], int k, int i, int & k){
for (int i = 0; i < n; i++){
if (p[i] > k){
k = p[i];
}
}
Your function passes in a variable i, and then you never use it; you create a whole new one called i inside the function. What's the point?
Edit: Please don't change the code in the original post. it makes the thread hard to follow when people's comments are about code that no longer exists.
So I fixed the array thing, deleted the 'i' variable, and only the 1st line was supposed to be in the loop, so i put the brackets there, maybe it is clearer now.
The thing is that the variable 'k' is repetetive. I use it in the start of the function and also I get the new value of 'k' as a result, so I get errors:
1.(in line 5 and 33) conflicting declaration 'int& k'|
2.(in line 16)invalid conversion from 'int' to 'int*' [-fpermissive]|
Inside this function, when you use the variable k , do you mean the second parameter or the fourth parameter? How is the compiler meant to know which you mean?
Do you understand why this code makes no sense:
1 2 3
int a;
float a;
cout << a; // WHICH a is being output?
That's the mistake you're making. Two variables with the same name.
What makes your code hard to manage is the first part:
1 2 3 4
int main()
{
int n, b = 0,k = 0;
int p[100], sum = 0, i= 0;
You declare all the variable at the beginning of the function, so that you make it hard to remember which one you used and for which purpose. I strongly suggest you to avoid that bad habit. Declare variables only where you really need them.
For example: where do you use ‘b’?
Please, have a look at the following code. It compiles but perhaps doesn’t do what you’d like it to do:
#include <fstream>
#include <iomanip>
#include <iostream>
// void funkcija(int p[], int k, int& k); // <-- two arguments with the same name
void funkcija(int p[], int n, int& k);
int main()
{
// read from file
std::ifstream fd ("duota.txt");
int n {};
fd >> n;
int p[100] {};
for (int i = 0; i < n ; i++){
fd >> p[i];
}
fd.close();
int k = 0;
funkcija(p, n, k); // k will be modified by the function
// write to file
std::ofstream fr ("rezultatai.txt");
for (int i = 0; i < n ; i++) {
if (p[i] == k){
fr << i+1 << " ";
}
elseif (k == 0) {
fr << "nera";
}
}
int sum = 0,
b = 0;
if (sum > 0) {
// integer division: it returns an integer
fr << std::fixed << std::setprecision(0) << sum / (n - b) << '\n';
}
else {
fr << "nera\n";
}
fr.close();
return 0;
}
// void funkcija(int p[], int k,, int & k) <-- double comma
void funkcija(int p[], int n, int& k)
{
for (int i = 0; i < n; i++) {
if (p[i] > k) {
k = p[i];
}
}
}