Hoang Son
Hoang Son

Reputation: 27

The parameter lost value after a loop

void genomicStatistic(int numberOfRow, int numberOfColumn, char arr[7][8]) {
int P[4][100];
char C[100];
int max = 0;
// 0. Set the initial value 

for (int i = 0; i < numberOfRow; i++) {
    for (int j = 0; j < numberOfColumn; j++) {
        P[i][j] = 0;
        cout << numberOfRow << endl;
        cout << numberOfColumn << endl;
    }
}}


void main() {
char arr[7][8]{
    {'A' ,'T' ,'C' ,'C', 'A',' G', 'C', 'T'},
    {'A' ,'T' ,'C' ,'C', 'A',' G', 'C', 'T'},
    {'A' ,'T' ,'C' ,'C', 'A',' G', 'C', 'T'},
    {'A' ,'T' ,'C' ,'C', 'A',' G', 'C', 'T'},
    {'A' ,'T' ,'C' ,'C', 'A',' G', 'C', 'T'},
    {'A' ,'T' ,'C' ,'C', 'A',' G', 'C', 'T'},
    {'A' ,'T' ,'C' ,'C', 'A',' G', 'C', 'T'},

};
genomicStatistic(7, 8, arr);   }

At // 0. , I try to set the initial value for array but after complier finish loop, numberOfRow and numberOfColumn is 0. I couldn't make sense why it happen, please help.

Upvotes: 0

Views: 223

Answers (2)

schorsch312
schorsch312

Reputation: 5714

int P[4][100]; is defined for values from 0..3 for the first index. You use values from 0..6. P and C are superfluous anyway.

By the way you are not really using C++. This is a more C++ way

#include <iostream>
#include <vector>
#include <string>

void genomicStatistic(const std::vector<std::vector<std::string>> &arr) {   
    for (const auto &row : arr) {
        for (const auto item : row) {
            std::cout << item << " ";
        }
        std::cout << std::endl;
    }
}

void main() {
    std::vector<std::vector<std::string>> arr{
        {"A", "T", "C", "C", "A", " G", "C", "T"}, 
        {"A", "T", "C", "C", "A", " G", "C", "T"},
        {"A", "T", "C", "C", "A", " G", "C", "T"},
        {"A", "T", "C", "C", "A", " G", "C", "T"},
        {"A", "T", "C", "C", "A", " G", "C", "T"},
        {"A", "T", "C", "C", "A", " G", "C", "T"},
        {"A", "T", "C", "C", "A", " G", "C", "T"},

    };
    genomicStatistic(arr);
}

Upvotes: 3

Yiao SUN
Yiao SUN

Reputation: 978

Because you work with a copy, when you finish your function this copy will delete (C and C++ in parameter use copy to pass values don't like java or others languages use reference ) and you should use reference or pointer in your parameter.

Upvotes: 0

Related Questions