Write a complete function:
DNA represented as a list of the alphabet letters A, C, G and T.

How many times the sequence ACG occurs in a specific DNA string.
(An example: If the list is CACGTTGCGTACGAA, then the number of occurrences of ACG will be 2.)

Write function nrACGOccur. Do not use any string member functions.

Assume the following:
• a global declaration:
const int numChars = 50; ; // number of characters in the array
• local declarations in the main function
char geneticArray[numChars]; // the array representing DNA
int nrOccur; ; // number of occurrences of ACG
• values have been assigned to all the elements in the array geneticArray
• the function is called in the main function as follows:
nrOccur = nrACGOccur(geneticArray);
Write down ONLY the complete function nrACGOccur.

This is what I did but unable to run the program - This is the 2 warnings I get
error C:\unisa\cos111\oct2007quest4.cpp:9
non-lvalue in increment

error C:\unisa\cos111\oct2007quest4.cpp:11
ISO C++ forbids comparison between pointer and integer

# include <iostream>
using namespace std;

const int NUM_CHARS =50;

char nrACGOccur(char genArray[])
{
    int nrOccurP = 0;
        for (int i= 0; i < NUM_CHARS; 1++)
            {
            if (genArray[i] == "ACG")
               nrOccurP++;
            }
        return nrOccurP;
}
int main ( )
{
    char geneticArray[] = {"CACTGACGACGCCGTACGCCTTGG"};
    int nrOccur;
    
    nrACGOccur(geneticArray);
     cout << nrOccur << endl;
     
     return 0;
}

Check your fingers for typos.
You're doing "ONE-PLUS-PLUS", not "eye-plus-plus"

> • the function is called in the main function as follows:
> nrOccur = nrACGOccur(geneticArray);
And do you do that?
Or do you just call it and ignore the result?

What is this:

if (genArray[i] == "ACG")

genArray == some_character, not some_string!

The code isn't that simple... You have to check char by char if 'A' then 'C' then 'G' occurs!

And of course, typos that Salem told you :)

Thank, missed those. I made the changes. The warning not allowed to make comparison between pointer and integer still present.

# include <iostream>
using namespace std;

const int NUM_CHARS =50;

char nrACGOccur(char genArray[])
{
    int nrOccurP = 0;
        for (int i= 0; i < NUM_CHARS; i++)
            {
            if (genArray[i] == "ACG")
               nrOccurP++;
            }
        return nrOccurP;
}

int main ( )
{
    char geneticArray[] = {"CACTGACGACGCCGTACGCCTTGG"};
    int nrOccur;
    
    nrOccur = nrACGOccur(geneticArray);
     
     
     return 0;
}

It has already been pointed out to you. if (genArray[i] == "ACG") This will not work, your are comparing a char* with a string. As it has already been pointed out.

I'll just restate the fact you need to check character by character

if (genArray[i] == "A"){
  if (genArray[i+1] == "C"){
    if (genArray[i+2] == "G"){
       std::cout << "Found 1!"
    }
  }
}

very simple way

It has already been pointed out to you. if (genArray[i] == "ACG") This will not work, your are comparing a char* with a string. As it has already been pointed out.

I'll just restate the fact you need to check character by character

if (genArray[i] == "A"){
  if (genArray[i+1] == "C"){
    if (genArray[i+2] == "G"){
       std::cout << "Found 1!"
    }
  }
}

very simple way

And of course, to write 'A', 'C', 'G', not "A", "C", "G"

Thanks, got to 13 times and then 0 without the i+the something.

or (int i= 0; i < NUM_CHARS; i++)
            {
            if (genArray[i] == 'A')
               if (genArray[i+1] == 'C')
                  if (genArray[i+2] =='G')
               nrOccurP++;
char geneticArray[] = {'C','A','C','T','G','A','C','G','A','C',
                           'G','C','C','G','T','A','C','G','C','C',
                           'T','T','G','G','A','G','G','G','A','C',
                           'G','A','A','G','C','G','G','T','T','A',
                           'A','T','T','A','A','G','G','T','C','A'};

Thank you all for your help and time. :cool:

This question has already been answered. Start a new discussion instead.