Elise Riplie
Elise Riplie

Reputation: 29

Using a Variable as an Array Parameter in C++

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;
}

Upvotes: 2

Views: 123

Answers (3)

Fabio Carballo
Fabio Carballo

Reputation: 364

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];

Upvotes: 2

Harry
Harry

Reputation: 11638

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;
}

Upvotes: 2

Cheers and hth. - Alf
Cheers and hth. - Alf

Reputation: 145269

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.

Upvotes: 2

Related Questions