Lab Assignment Help Needed.

Write your question here.

I put together this program for a lab in class. The task is to generate a array of random characters of a size determined by the user. After that we have to write a function that takes in that array and prints out the highest consecutive occurence of a character within that array.

for example if the array is aaaabbcc
the result is aaaa.

I was able to get pretty far until I moved my code around. All I did was copy and past some code in the program to put it into a function called OccChar. Now I get a error telling me "expected primary expression before ']' token on LINE 33

P.S. This is for school. Looking to major in COMPUTER SCIENCE.
Thank you.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
  #include <iostream>
#include<cstdlib>
#include <time.h>
#include <cstring>

using namespace std;


char generateRandom();
void OccChar(char a[],int arraySize);


int main(){

int Size;
cout<<"How big do you want this array to be"<<endl;
cin>>Size;
char c[Size];
float cTotal = 0;
float cPerc;
// Fill characters in to the array c


for(int n=0; n<Size; n++){

    c[n] = generateRandom();
    if (c[n] == 'c')
        cTotal ++;

    }
cPerc = (((cTotal)/Size)*100);
cout<<"There amount of C's in this string are "<<cPerc<<"% of the entire string"<<endl;
OccChar(c[],Size);


for (int i=0;i<Size;i++){
    cout<<c[i]<<endl;
}

return 0;
}

char generateRandom() {

    int RandomNumber = rand() % 4;
    char Alpha = (char)('a' + RandomNumber);
    return Alpha;
}

void OccChar(char a[], int arraySize){

int start = -1;
int bestStart = -1;
int bestLength = 0;
int currentLength = 0;
for (int i = 0; i < arraySize; ++i) {
    if (a[i] == 'b') {
        if (start == -1) {
            start = i;
        }
        ++currentLength;
    } else {
        if (currentLength > bestLength) {
            bestStart = start;
            bestLength = currentLength;
        }
        start = -1;
        currentLength = 0;
    }
}
    for(int i=0;i<bestLength;++i){
    cout<<a[bestStart];
    }

}
When you pass an array to a function, you do not include the brackets. The error message is telling you exactly what the issue is- you put in brackets on line 33 next to the array pointer when you did not need to.
Last edited on
Topic archived. No new replies allowed.