2017-07-07 34 views
0

下面的代码:在下面的代码中,为什么不在输出中显示数组的值?

#include<iostream> 
using namespace std; 


int main() 
{ 
    int n, counter = 0; 
    cout << "how many markers do you have? "; 
    cin >> n; 
    int* Arr = new int[n]; //markers 
    int* cnt = new int[n]; 


    cout << "enter your marker color numbers here" << endl << " choose between 1 to 100: " << endl; 
    for (int j = 0; j < n; j++) 
     cin >> Arr[j]; 


    for (int i = 0; i < n; i++) 
{  for (int j = 1; j <= 100; j++) 
      if (Arr[i] == j) 
      { 
       counter = j; 
       cnt[counter--] = counter + cnt[counter--]; 
      } 
      else 
      { 
       j++; 
      } 
} 

    for (int f = 0; f < n; f++) 
     cout <<endl<<endl<<endl<<"here is the cnt "<<f<<": "<<cnt[f]<<"  "; 

    return 0; 
} 

,并在输出CNT的替代细胞的价值观,我得到的垃圾值。 我希望它将计数器添加到cnet数组的值。

+2

你怎么能不知道你正在编程的编程语言? – Lundin

+0

你有没有调试你的程序? –

回答

0

cnt[counter--] = counter + cnt[counter--]; 

的用法是错误的,那就是,你正在试图指的是碳纳米管阵列不与分配的指数。 因为cnt数组声明的大小为n。 Cnt数组获取从0到n-1的分配。 let let if if n == 3,Your cnt array will have the storage space from cnt [0] to cnt [n-1]。 在你的第二个for循环中,如果counter == 8,那么cnt [8]没有被分配,它只是抛出一些垃圾值,希望你明白。

+0

即将发布类似于此的答案。 –

+0

我必须补充的一点是,counter--是一个操作,因此会影响整个变量并更改变量值,而不是操作它所代表的值。 –

+1

而且计数器变量也会在那一行中递减两次,我不这么认为,不需要两次去计数器变量。 – Goutham

相关问题