Using a Variable as an Array Parameter in C++ - 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;
}

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.

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

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

Related

How to end a user input array

So this is for a lab assignment and I already have it working, but one thing is bothering me. The assignment involves creating a 1-dimensional array and then manipulating it. I am supposed to allow a max of 100 inputs but the user does not have to use all 100. Right now, I am using a while statement to either break or allow another input to be entered. To break the statement, you have to enter a negative number (this is what I don't like and want to change). What other options are there to end the user input, once they are done entering their numbers? Is it possible to end the loop once you hit enter with nothing typed?
I have searched stackoverflow for the last 3 days and found some compelling stuff but could never get it to work.
Note, I get the void function is redundant here but that's besides the point (unless it actually affects my ability to achieve what I want).
Also, thanks in advance.
here is my code so far (my while statement is in the main)... be kind I'm a newbie to coding.
#include <iostream>
using namespace std;
void reverseElements(int array[], int size)
{
int tmp;
int j;
int i = size;
j = i - 1;
i = 0;
while (i < j)
{
tmp = array[i];
array[i] = array[j];
array[j] = tmp;
i++;
j--;
}
cout << "I will now reverse the elements of the array." << endl;
cout << endl;
for (i = 0; i < size; i++)
{
cout << array[i] << " " << endl;
}
}
int main()
{
const int NUM_ELEMENTS = 100;
int iArr[NUM_ELEMENTS];
int i;
int myInput;
cout << "Enter your numbers, then enter a negative number to finish" << endl;
cout << endl;
for (i = 0; i < NUM_ELEMENTS; i++) //loop to obtain input
{
cin >> myInput;
if (myInput < 0) //checks for negative number to end loop
{
break;
}
else //continues to allow input
{
iArr[i] = myInput;
}
}
cout << endl;
reverseElements(iArr, i);
return 0;
}
Probably the easiest solution: let your user choose how many numbers to write before actually writing them.
int readNumbersCount()
{
int const numbersMin = 1;
int const numbersMax = 100;
int numbersCount = -1;
while (numbersCount < numbersMin || numbersCount > numbersMax)
{
std::cout <<
"How many numbers are you going to enter? Choose from " <<
numbersMin << " to " << numbersMax << ":\n";
std::cin >> numbersCount;
}
return numbersCount;
}
int main()
{
int const numbersCount = readNumbersCount();
for (int i = 0; i < numbersCount; ++i)
{
// read the numbers etc.
}
return 0;
}
I wrote readNumbersCount() as a separate function to extract numbersMin and other "one-use" identifiers from main() and to make main()'s numbersCount const.
I have edited the main function a little bit.
Here the user is asked how many elements he wants to enter .
and doing the memory allocation dynamically so as to save space
int main()
{ int n=101;
while(n>100){
cout<<"How many numbers do you want to enter";
cin>>n;
}
int *ptr=new(nothrow)int[n];
for (int i=0;i<n;i++){
cout << "Enter your number" << endl;
cin>>ptr[i];
}
cout << endl;
reverseElements(ptr, n);
return 0;
}

0xC0000005: Access violation writing location 0xCDCDCDCD Dynamic Allocation Error

Getting 0xC0000005: Access violation writing location 0xCDCDCDCD with the code below.
I know I must have not allocated the pointer properly, but I'm not sure where.
I'm trying to have **scoreSet refer to sets of *scores and *scores is to be entered in manually. The names pointer to array is working fine and seems to be allocated properly. The problem is when I try to mimic the same for scoreSet, with the difference being scoreSet is a pointer to an array of pointers scores. I feel like the way I'm trying to dynamically allocate the arrays that this pointer is pointing to is completely wrong.
Basically trying to get something like this to be possible after user input:
scoreSet0 = {22,33,44}
scoreSet1 = {35, 45, 65, 75}
scoreSet3 = {10}
#include <iostream>
#include <string>
using namespace std;
int inputData(string*& names, double**& scores);
int main() {
string *names = nullptr;
double *scores = nullptr;
double **scoreSet = &scores;
int size = 0;
size = inputData(names, scoreSet);
for (int i = 0; i < size; i++) {
cout << *(names+i) << endl;
}
}
int inputData(string*& names, double**& scoreSet) {
int numStudents = 0;
cout << "How many students do you have in the system? ";
cin >> numStudents;
while (numStudents <= 0) {
cout << "Invalid number of students. Please enter number of students: ";
cin >> numStudents;
}
names = new string[numStudents];
cin.ignore(10000, '\n');
for (int i = 0; i < numStudents; i++) {
int numTests = 0;
cout << "Enter the student's name: ";
getline(cin,names[i]);
cout << "Enter how many tests " << *(names + i) << " took: ";
cin >> numTests;
*(scoreSet + i)= new double[numTests]; //Pretty sure this is wrong.
cin.ignore(10000, '\n');
for (int j = 0; j < numTests; j++) { //This loop is causing the error.
cout << "Enter grade #" << j + 1 << ": ";
cin >> *(scoreSet+i)[j];
}
}
return numStudents;
}
Per PaulMcKenzie suggestion, this is how it would roll. It may be a bit much for you to use templates, but if you can... Otherwise create the name and score containers separately. But then you have duplicate code to maintain.
The idea is to keep all your stuff in some kind of order. Note that now the memory management is taken care of in the container.
I dropped handling std::cin and the scores, but it should be much easier for you to code that stuff back without a lot of fluff in the way. At that, develop without std::cin, it is a waste of time. You should write so you can just edit and run.
Also, get out of the habit of using namespace std; It will pay off in the long run.
#define DEV
template<typename T>
struct container {
size_t size;
T* ar;
container(size_t size) :size(size) {
ar = new T[size];
}
~container() { delete[]ar; }
T& operator [](size_t pos) { return ar[pos]; }
};
using names_c = container<std::string>;
using scores_c = container<double>;
size_t inputData(names_c& names, scores_c& scores);
int main() {
container<std::string> names(2);
container<double> scoreSet(2);
auto size = inputData(names, scoreSet);
for (int i = 0; i < size; i++) {
std::cout << names[i] << endl;
}
}
size_t inputData(names_c& names, scores_c& scores) {
#ifdef DEV
size_t numStudents = 2;
names[0] = "tom";
names[1] = "mary";
#else
//do your std::cin stuff
#endif
return names.size;
}
I wasn't going to go there, but. You can extend the concept so that you have containers in containers. Much easier to know what scores go with what student.
struct student_type {
using scores_c = container<double>;
std::string name;
scores_c scores;
};
using student_c = container<student_type>;
I've taken your code, and modified it to work. I've removed your comments, and placed comments in on the lines that I changed.
#include <iostream>
#include <string>
using namespace std;
int inputData( string *&names, double **&scores );
int main() {
string *names = nullptr;
double **scores = nullptr; // Changed to double ** so it's "2D"
// double **scoreSet = &score; // removed, this was unneeded and probably causing problems
int size = 0;
size = inputData( names, scores );
for ( int i = 0; i < size; i++ ) {
cout << *( names + i ) << endl;
}
}
int inputData( string *&names, double **&scoreSet ) {
int numStudents = 0;
cout << "How many students do you have in the system? ";
cin >> numStudents;
while ( numStudents <= 0 ) {
cout << "Invalid number of students. Please enter number of students: ";
cin >> numStudents;
}
names = new string[numStudents];
scoreSet = new double*[numStudents]; // allocate an array of pointers
// cin.ignore( 10000, '\n' ); // Removed from here, placed inside loop
for ( int i = 0; i < numStudents; i++ ) {
cin.ignore( 10000, '\n' ); // placed here so that it always clears before getting the name
int numTests = 0;
cout << "Enter the student's name: ";
getline( cin, names[i] );
cout << "Enter how many tests " << names[i] << " took: "; // simplified
cin >> numTests;
scoreSet[i] = new double[numTests]; // simpliefied left hand side
//cin.ignore( 10000, '\n' ); // not needed
for ( int j = 0; j < numTests; j++ ) {
cout << "Enter grade #" << j + 1 << ": ";
cin >> scoreSet[i][j]; // simplified
}
}
return numStudents;
}

why does my counter increase according to the lens of char input in C++

I'm picking up on C++ recently and is trying to code a program which prompts for names for a defined no. of times and inserts each of the input into an array of size-5. The problem happened when I tried to run the following code, my counter, i increases according to the no of len the user input. Why is that so?
#include <iostream>
using namespace std;
int main(){
const int SIZE = 5;
char name[SIZE];
int i;
for (i = 0; i < SIZE; i++){
if (strlen(name) <= 50) {
cout << "Enter a name: \n";
cin >> name[i];
}
}
for (i = 0; i < SIZE; i++){
cout << name[i] << endl;
}
return 0;
}
Output:
if (strlen(name) <= 50) {
You should not call strlen on array which is not initialized.
Use array of strings otherwise
cout << name[i] << endl;
refers to i-th character, not entire string. Or if you want to go with char arrays, you'd need a two dimensional array.
I thing what you indended to do was :
#include <iostream>
using namespace std;
int main(){
const int SIZE = 5;
string names[SIZE];
int i;
for (i = 0; i < SIZE; i++){
cout << "Enter a name: \n";
string name;
cin>>name;
if (strlen(name) <= 50) {
cin >> names[i];
}
}
for (i = 0; i < SIZE; i++){
cout << name[i] << endl;
}
return 0;
}
UNTESTED
The second for loop, which does the output, does this in single characters, incrementing i each time.
To output the string all at once assign a string pointer to name[0] and send that to cout.

can not swap array elements c++

I am new to C++. I am trying to solve a problem in the textbook: swap the first and last element in an array. But when I run the code I wrote, nothing happened and even the sentence "Please enter the numbers in the array: " does not show up. Anyone could give some help? Thanks.
#include <iostream>
using namespace std;
int swap(int values[], int size)
{
int temp = values[0];
values[0] = values[size-1];
values[size-1] = temp;
}
int main()
{
const int SIZE = 5;
int test[SIZE];
cout << "Please enter the numbers in the array: " << endl;
int input;
cin >> input;
for(int i=0; i<SIZE; i++)
{
test[i] = input;
}
swap(test, SIZE);
cout << test[SIZE] << endl;
return 0;
}
There were a few mistakes:
You should get the input inside the loop and then assign it to the test array.
When printing the swapped value, access the test array with SIZE-1 instead of SIZE, because array indexes run from 0 to SIZE-1, inclusive.
You declared swap() as returning int, but provided no return statement (this suggests that you haven't enabled enough warnings from your compiler).
#include <iostream>
using namespace std;
void swap(int values[], int size)
{
int temp = values[0];
values[0] = values[size-1];
values[size-1] = temp;
}
int main()
{
const int SIZE = 5;
int test[SIZE];
int input;
cout << "Please enter the numbers in the array: " << endl;
for(int i=0; i<SIZE; i++)
{
cin >> input;
test[i] = input;
}
swap(test, SIZE);
cout << test[SIZE-1] << endl;
return 0;
}
#include <iostream>
using namespace std;
//Here return type should be void as you are not returning value.
void swap(int values[], int size)
{
int temp = values[0];
values[0] = values[size-1];
values[size-1] = temp;
}
int main()
{
const int SIZE = 5;
int test[SIZE];
cout << "Please enter the numbers in the array: " << endl;
//USE LOOP TO TAKE INPUT ONE BY ONE IN AN ARRAY
for(int i = 0; i < SIZE; i++)
cin >> test[i];
swap(test, SIZE);
//USE LOOP TO DISPLAY ELEMENT ONE BY ONE
for(int i = 0; i < SIZE; i++)
cout << test[i] << endl;
return 0;
}

Access violation writing location 0x00000000. problems with pointers

I'm having trouble with some code for an assignment. The program is based on an online contest problem archive for college students.
Here is the header file:
#include <iostream>
#include <cmath>
using namespace std;
class Vito
{
public:
Vito(int relative_count); //constructor
~Vito(); //destructor
int min(); //finds minimum "distance" between vito's relatives
private:
int *streets; //pointer to array of street numbers
int sum(int index); //generates sum of distances for each of vito's relatives
void getStreetNums(); //retrieves street numbers from user
int relatives; //used globaly to set number of repititions
};
Vito::Vito(int relative_count = 0)
{
int *streets = new int[relative_count]; //allocates memory for array streets
relatives = relative_count;
getStreetNums();
}
Vito::~Vito()
{ delete [] streets; } //releases memory used by class
void Vito::getStreetNums()
{
cout << "Enter all street numbers, seperated by a space: ";
int street_num;
for (int i = 0; i < relatives; i++)
{
cin >> street_num;
streets[i] = street_num;
}
}
int Vito::min()
{
int MIN = 65546, test_distance; //initialized to maximum possible value for an integer in C++
for (int i = 0; i < relatives; i++)
{
test_distance = sum(i);
if( MIN > test_distance )
{ MIN = test_distance; }
}
return MIN;
}
int Vito::sum(int index)
{
int SUM = 0, street_num;
street_num = *(streets+index); //set value for determining distances between one house and the others
for (int i = 0; i < relatives; i++)
{ SUM += abs( street_num - streets[i] ); }
return SUM;
}
Here is the main:
#include <iostream>
#include "proj_02.h"
using namespace std;
int main()
{
int relatives, tests;
cout << "This program will supply a minimum distance between homes based on a given number of relatives and street numbers their homes are on. All entered values must be integers." << endl << endl;
cout << "Enter how many tests will be run: ";
cin >> tests;
for (int i = 0; i < tests; i++)
{
cout << "Enter how many relatives will be used in test " << i+1 << ": ";
cin >> relatives;
Vito family(relatives);
cout << "For this case, the minimum distance between each relatives house compared to each other is: " << family.min() << endl << endl;
}
}
After execution I get an error message saying "0xC0000005: Access violation writing location 0x00000000." here:
void Vito::getStreetNums()
{
cout << "Enter all street numbers, seperated by a space: ";
int street_num;
for (int i = 0; i < relatives; i++)
{
cin >> street_num;
streets[i] = street_num;
}
}
debugging shows that there is a null memory address set for streets, but I allocated memory for it earlier in the constructor. Can anyone explain what's going on here?
Vito::Vito(int relative_count = 0)
{
int *streets = new int[relative_count]; //allocates memory for array streets
In the constructor, you defined a local variable streets, and allocate memory for it. The local variable streets shadows the class member streets. Change it to:
Vito::Vito(int relative_count = 0)
{
streets = new int[relative_count]; //allocates memory for array streets