简体   繁体   中英

what will be output of this code? in line 13 what is really happening?

#include <iostream>
#include <string.h>
using namespace std;
int main() {

    int num , n , n1 , k , x = 0;
    cout << "Enter the no. of vertices: " ;
    cin >> n;
    char StorageArr[n];
    char arr[k];
     for(k = 0; k< n ; k++){
       cout<< "arr[" << k << "] : ";
       cin >> arr[k] ;
       StorageArr[n] = arr[k];
    }

    int len = sizeof(StorageArr)/ sizeof(StorageArr[0]);

    cout << "The length of the array is : " << len << endl;
     for(int i= 0 ; i< len ; i++){
       for(int j = 0; j< len ; j++ ){
            cout<<"\n\t" <<"Element at " << StorageArr[i] <<        StorageArr[j] << ": "  ;
        cin >> num;
        if(i==j && num == 0){
            x++;
        }
    }
}

if(x == len){
    cout<<"This  is a complete graph...";
}else{
    cout<< "This is not a complete graph..." ;
}

return 0;

}

ok so how is StorageArr[n] = arr[k] is working. I mean if we assign n = 5 then all the value of arr[k] will be assign to the same position StorageArr[5] or will it be that the value of arr[k] will store in StorageArr as like first on 0th pos then on 1st postion and then so on till 4th....

If n == 5 then

StorageArr[n] = arr[k];

will assign arr[k] to StorageArr[5] always. That's what the code says and so that's what it will do (in a working program). I'm not sure where you got the idea that it might assign to StorageArr[0] then StorageArr[1] and so on.

There are many errors in your code, to mention just one, look at the code above, what is the value of k ? At no point do you give it a value. But you use it's value all over the place. That means your whole program has undefined behaviour, and it's pointless trying to work out what it will do.

Fix the errors and your program will behave in a more predictable way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM