2

I'm trying to create something similar to string.find with vectors c++98, but I can't fix this error. I have simplified the code and left only the error. If I can get help on how to achieve this.

#include <iostream>
#define MAX 99999
using namespace std;

int free_pos (int v[]);
int find_line (int v[], int pos);

int main() {
   char text[MAX];
   int loc_key[MAX],start_line[MAX]; //

   for(int i=0; i<free_pos(loc_key);i++) {
       cout << "Line: " << i+1;
       int pos = find_line(start_line[],loc_key[i]); //Here is the error
       for(int j=start_line[pos];j<start_line[pos+1];j++) {
            cout<<text[j];
       }
       cout<<endl;
   }
   return 0;
}

int free_pos (int v[]) {
    for(int i=0;i<MAX;i++){
        if(v[i] == 0)
            return i;
    }
    return 99;
}

int find_line (int v[], int pos) {
    for(int i=0; i<free_pos(v); i++) {
        if(v[i]==pos)
            return v[i];
        if(v[i]< pos)
            return v[i-1];
    }
}
Ahmad
  • 1,618
  • 5
  • 24
  • 46
Alex
  • 58
  • 1
  • 7

2 Answers2

2

Your both functions

int free_pos (int v[]);
int find_line (int v[], int pos);

take arrays as an input, but then you try to call int pos = find_line(start_line[],loc_key[i]);, which should take the name of an array only, as it is the name which is known by the program as an array. You can easier (in my opinion) get it, if you write both of these functions following way:

int free_pos (int* v);
int find_line (int* v, int pos);

They do the same job as your functions, but you can see that their arguments are: an int pointer (array) and an int. Then, calling the function needs only the int pointer, which in your case is start_line, and the integer loc_key[i]. If you still have problems understanding it, you could read about dynamic memory allocation of arrays, which is done as: int* arr; arr = new int[your_size]; - I think it is clearer while thinking that way.

whiskeyo
  • 873
  • 1
  • 9
  • 19
0
#include<bits/stdc++.h>
using namespace std;
int max(int arr[],int n){
maximum=INT_MAX;
for(int i=0;i<n;i++){
if(arr[i]>maximum){
maximum=arr[i]}}
return maximum;}

int main(){
int size;
cin>>size;
for(int i=0;i<size;i++){
cin>>arr[i]}
cout<<max(arr,size);
return 0;}
xp4
  • 1
  • 1
    1. ***Never*** recommend the use of `#include` in an answer on Stack Overflow; read [this](https://stackoverflow.com/q/31816095/10871073) and then replace that line with the relevant **standard** headers. 2. Add some indentation and decent formatting to your code. 3. Add an explanation as to why/how your code fixes the OP's problem. – Adrian Mole Sep 01 '22 at 19:50