2

I am trying to write code that will compare a 10 sequence piece of DNA to that of a relative's 10 sequence DNA. The user inputs their name, how many relatives they want to compare, and their DNA. The computer outputs the percentage of a match. ATTAGACGCA compared to ATAAGACGCA would match 90%. The number of relatives is a constant after the user states how many relatives. I have tried using const, but it doesn't seem to want to use the number.

/**********************************************************************
 * Get DNA Sequence
 ***********************************************************************/
void getMyDNA(char myDNA[])
{
   cout << "Enter your DNA sequence: ";
   cin >> myDNA;
}

/**********************************************************************
 * Get Potential Relatives
 ***********************************************************************/
int getRelatives()
{
   int relatives = 0;
   cout << "Enter the number of potential relatives: ";
   cin >> relatives;

   return relatives;
}

/**********************************************************************
 * Get Potential Relatives Names
 ***********************************************************************/
void getRelativeName(string relativeNames[], int relatives)
{
   string name;
   for (int i = 0; i < relatives; i++)
   {
      cout << "Please enter the name of relative #" << i + 1 << ": ";
      cin >> name;
      relativeNames[i] = name;
   }
}

/**********************************************************************
 * Get Potential Relatives DNA Sequence
 ***********************************************************************/
void getRelativeDNA(char relativeDNA[][10], string relativeNames[], int relatives)
{
   for (int i = 0; i < relatives; i++)
   {
      cout << "Please enter the DNA sequence for " << relativeNames[i] << ": ";
      cin >> relativeDNA[i];
   }
}

/**********************************************************************
 * Display Potential Relatives Match
 ***********************************************************************/
void displayMatch(string relativeNames, char relativeDNA[][10], int relatives, char myDNA[])
{
   const int family = relatives;
   int count[family] = 0;
   for (int r = 0; r < 3; r++) //relative number r
   {
      for (int d = 0; d < 10; d++) //dna piece number d
      {
         if (relativeDNA[r][d] == myDNA[d])
            count[r]++;
      }
   }
}

/**********************************************************************
 * Main
 ***********************************************************************/
int main()
{
   char myDNA[10];
   string relativeNames[50];
   char relativeDNA[50][10];

   // My DNA
   getMyDNA(myDNA);


   //# of relatives
   int relatives = getRelatives();
   cout << endl;

   //thier names
   getRelativeName(relativeNames,relatives);
   cout << endl;


   //their dna
   getRelativeDNA(relativeDNA,relativeNames,relatives);
   cout << endl;

   //display
   displayMatch(relativeNames,relativeDNA,relatives,myDNA);

   return 0;
}
3
  • C++ doesn't have variable-length arrays so technically that's not valid C++. If you want a variable-length array use std::vector. Commented Apr 23, 2016 at 2:18
  • Also, what is count supposed to do? Is it an array or not? Commented Apr 23, 2016 at 2:20
  • I updated my question to include my entire code. Count is supposed to count how many letters in the sequence of DNA between me and my relative are the same. Commented Apr 25, 2016 at 19:22

3 Answers 3

2

Instead of

int count[relatives] = 0;

which is invalid as standard C++ when relatives can vary at run-time, use

std::vector<int> count( relatives );

Include the <vector> header.

Sign up to request clarification or add additional context in comments.

2 Comments

Even if relatives couldn't vary, wouldn't it still be invalid code? You can't assign an entire array to an integer.
@MatthewCliatt: Yes, the initializer would have to be fixed, like {0} (or just {}).
2

If count is a new array create a new array dynamically as follows...

int *count = new int[relatives];

I noticed you're using the following later...

count++;

Are you trying to increment an integer or move a pointer? This code might help make it clearer...

#include <assert.h>
#include <iostream>
#include <typeinfo>

int main(void) {
  const int x = 500;
  int* a = new int[x];
  size_t i = 0;
  for(i = 0; i < x;i++) {
    a[i] = i;
  }

  for(i = 0; i < x;i++) {
    //Print numbers without moving pointer
    std::cout << a[i] << std::endl;
  }
  for(i = 0; i < x;i++) {
    //Print numbers moving pointer
    std::cout << a[0] << std::endl;
    a++;
  }
  a = a - x;
  delete[] a;
  return 0;
}

3 Comments

Advising a beginner to use new for array is diabolic.
And no mention of using delete[ ].
I'll fix it up in a tick
2

Even if the parameter is passed as const it won't work. You can try with an array created dynamically

int *count = new int[relatives];

2 Comments

Please do not use the term vector to describe what you're advising the OP.
I'm not sure "dynamic array" is a good term for this. Its size is variable, but the array is in no way dynamic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.