Currently I'm having an issue with a homework problem although I'm not asking for the answer. I Just want to see if anyone is able to help me trouble shoot my issue...

Issue:
- ArrayPointer.cpp: In function ‘int* duplicate(int*, int)’:
- ArrayPointer.cpp:62: error: expected `;' before ‘list’
- Line 6

Task:
- I'm trying to write a program that will take numbers from an array and display them and calculate the average.
- Although i do have a separate function that is to duplicate the array and then just display it a second time although the issue is within my duplicate function.
- I'm also utilizing an array as a pointer... I'm not even sure what that is to be honest. I'm guessing it is designating an array that I've created to be used within multiple functions. If you could describe further that would be awesome.

Utilizing:
- Mac OSX
- Compiling in Terminal

// Duplicate Array
int *duplicate(int *list, int count)
{
    int *duplicate;
    
    duplicate = new int list[MAX]; 
    
    for(int i = 0; i < count; i++)
    {
        duplicate[i] = list[i]; 
    }
    return duplicate; 
}

Thank you,

CanaznFTW

Edited 5 Years Ago by CanaznFTW: n/a

Okay I tried this but still receiving errors.

// Duplicate Array
int *duplicate(int *list, int count)
{
    int *dup;
    
    dup = new int list[MAX]; 
    
    for(int i = 0; i < count; i++)
    {
        dup[i] = list[i]; 
    }
    return dup; 
}

Does that look correct?

Errors:
- ArrayPointer.cpp: In function ‘int* duplicate(int*, int)’:
- ArrayPointer.cpp:62: error: expected `;' before ‘list’

Defined:
- Line 53
- I know that the semicolon is minor but I can't find the issue... I'm also concerned about the duplicate function

Thanks for your help

***Entire Program***

#include <iostream>

using namespace std;

// Functions 
void initialize(int *list, int &count);
int *duplicate(int *list, int count);
void display(int *list, int count);
double average(int *list, int count);


// Main Program 
int main()
{
   const int MAX = 100;
   int list1[MAX];
   int *list2;
   int count;
   initialize(list1, count);
   list2 = duplicate(list1, count);
   cout << "Original List:\n";
   display(list1, count);
   cout << "Duplicate List:\n";
   display(list2, count);
   cout << "Average: " << average(list1, count) << endl;
   return 0;   
}

// Function Definitions 
void initialize(int *list, int &count)
{
    const int SENTINEL = -1;
    int num; 
    
    count = 0; 
    cout << "Please enter integers (-1 to quit). ";
    cin >> num; 
    cout << endl; 
    
    while(num != SENTINEL)
    {
        list[count] = num; 
        count++;
        cin >> num; 
    }
}

// Duplicate Array
int *duplicate(int *list, int count)
{
    int *dup;
    
    dup = new int list[MAX]; 
    
    for(int i = 0; i < count; i++)
    {
        dup[i] = list[i]; 
    }
    return dup; 
}

// Display the Array
void display(int *list, int count)
{
    for(int i = 0; i < count; i++)
    {
        cout << list[i] << endl;  
    }
}

// Calculate the Average 
double average(int *list, int count)
{
    double average = 0; 
    int sum = 0;  
    for(int i = 0; i < count; i++)
    {
        sum += list[i];
    }
    average = sum / (float)count; 
    return average; 
}

Edited 5 Years Ago by CanaznFTW: n/a

You have two problems, both on the same line.

dup = new int list[MAX];

The first problem is that when you are using new to make an array, you need [] around the size of the array you wish to create. The second problem is that MAX isn't visible in the scope of the duplicate function. After fixing that, I got it to compile, though I won't vouch for the correctness of your program.

It has to be

dup = new int[MAX];

not

dup = new int [B]list[/B][MAX];

And yes, MAX is currently not known in duplicate().

I apologize everyone and do appreciate your information although trying your solutions I am still receiving the same errors...

where did you define MAX?

He defined it in main, but he's not passing it as a parameter for any of his functions. Either define the variable as a global variable (not recommended as a habit) or pass the value as a parameter in your functions.

--EDIT--

I got your program compiling and it *appears* to work, but as for the acutal correctness of the code, not sure. I didn't bother to look through any of it, just moved the constant integer MAX outside the main function and turned it into a global variable.

#include <iostream>

using namespace std;

// Functions 
void initialize(int *list, int &count);
int *duplicate(int *list, int count);
void display(int *list, int count);
double average(int *list, int count);

const int MAX = 100;  // global variable

// Main Program 
int main()
{
   int list1[MAX];
   int *list2;
   int count;
   initialize(list1, count);
   list2 = duplicate(list1, count);
   cout << "Original List:\n";
   display(list1, count);
   cout << "Duplicate List:\n";
   display(list2, count);
   cout << "Average: " << average(list1, count) << endl;
   return 0;   
}

// Function Definitions 
void initialize(int *list, int &count)
{
    const int SENTINEL = -1;
    int num; 
    
    count = 0; 
    cout << "Please enter integers (-1 to quit). ";
    cin >> num; 
    cout << endl; 
    
    while(num != SENTINEL)
    {
        list[count] = num; 
        count++;
        cin >> num; 
    }
}

// Duplicate Array
int *duplicate(int *list, int count)
{
    int *dup;
    
    dup = new int list[MAX]; 
    
    for(int i = 0; i < count; i++)
    {
        dup[i] = list[i]; 
    }
    return dup; 
}

// Display the Array
void display(int *list, int count)
{
    for(int i = 0; i < count; i++)
    {
        cout << list[i] << endl;  
    }
}

// Calculate the Average 
double average(int *list, int count)
{
    double average = 0; 
    int sum = 0;  
    for(int i = 0; i < count; i++)
    {
        sum += list[i];
    }
    average = sum / (float)count; 
    return average; 
}

Edited 5 Years Ago by floatingDivs: n/a

I appreciate everyone's help although I believe it might be my compiler... I've tried to even copy and paste your code then compile and I'm still receiving the same error,


ArrayPointer.cpp: In function ‘int* duplicate(int*, int)’:
ArrayPointer.cpp:63: error: expected `;' before ‘list’

I'm not to sure as to why I'm still having this issue. This normally is stating that within my duplicate function I'm missing a semicolon or might have accidentally put one in the wrong spot. Although I don't see any issues.

I'm going to attempt to compile this with Visual Studio and see if that will fix the issue.

I appreciate everyone's time. Thank you,

CanaznFTW

The code posted by FloatingDivs still has the original problem that you had (on line 53). If you change that to dup = new int[MAX] then it should go away. Otherwise, you should try a new compiler.

Edited 5 Years Ago by ravenous: n/a

>>Otherwise, you should try a new compiler.
Don't blaim the compiler for your own failed code. You still have not fixed what other people have told you is wrong with that function.

>>Otherwise, you should try a new compiler.

A different compiler won't help. The only want to get rid of the error is to fix the problem.

Apologies for the broken code. I fixed it in my Terminal, but because of my incompetence (not knowing how to CUT out an entire "file" *facepalm*) I was forced to rewrite a quoted copy of your code and forgot to fix the issue on line 53.

This article has been dead for over six months. Start a new discussion instead.