2012-04-25 3 views
0

Это базовый код для массива, который я пишу. Мне нужно заполнить все слоты (14) массива с помощью 4, а затем написать цикл, который заменит слоты 6 и 13 на 0. Я новичок и еще не изучил векторы, просто базовый материал для программирования.C++ заполняющие массивы с постоянными значениями, циклические и изменяющиеся значения

const int MAX = 14; 

int main() 
{ 

    board(); 
    cout<<endl; 

    { 


     int i; 
     int beadArray[MAX] = {4}; 

     for (i = 0; i < MAX; i++) 
     { 
      beadArray[i] = -1; 
     } 

     for (i = 0; i < MAX; i++) 
     { 
      cout<<i<<"\t"; 
     } 
    } 


    cout<<endl; 
    system("pause"); 
    return 0; 
} 
+0

чем проблема вы съели перед? – Naveen

+3

Это, кажется, точный дублирующий вопрос о том, что вы опубликовали 30 минут назад ... http://stackoverflow.com/questions/10308932/filling-an-array-with-the-same-value-looping-to- reset-values ​​ –

+0

@JamesCuster Отмечено для него. – twain249

ответ

0

вы могли бы сделать что-то вроде этого

int beadArray[14]; 
for(int i=0;i<14;i++){ 
    beadArray[i]=4; 
} 
beadArray[6]=0; 
beadArray[13]=0; 

или

int beadArray[14]; 
for(int i=0;i<14;i++){ 
    if(i==6 || i==13) 
     beadArray[i]=0; 
    else 
     beadArray[i]=4; 
} 
2
#include <iostream> 
#include <cstdlib> 
using namespace std; 

int main(){ 

    //this constant represents the size we want our array to be 
    //the size of an array must be determined at compile time. 
    //this means you must specify the size of the array in the code. 
    const unsigned short MAX(14); 

    //now we create an array 
    //we use our MAX value to represent how large we want the array to be 
    int beadArray[MAX] = {4}; 

    //now our array looks like this: 
    //+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ 
    //| 4 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 
    //+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ 

    //but you want each index to be filled with 4. 
    //lets loop through each index and set it equal to 4. 
    for (int i = 0; i < MAX; ++i){ 
     beadArray[i] = 4; 
    } 

    //now our array looks like this: 
    //+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ 
    //| 4 | 4 | 4 | 4 | 4 | 4 | 4 | 4 | 4 | 4 | 4 | 4 | 4 | 4 | 
    //+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ 

    //to set slots 6 and 13 equal to 0, it is as simple as this: 
    beadArray[6] = 0; 
    beadArray[13] = 0; 

    //careful though, is that what you wanted? 
    //it now looks like this: 
    //+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ 
    //| 4 | 4 | 4 | 4 | 4 | 4 | 0 | 4 | 4 | 4 | 4 | 4 | 4 | 0 | 
    //+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ 

    //this is because the [index number] starts at zero. 

    //index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 
    //  +---+---+---+---+---+---+---+---+---+---+---+---+---+---+ 
    //  | 4 | 4 | 4 | 4 | 4 | 4 | 4 | 4 | 4 | 4 | 4 | 4 | 4 | 4 | 
    //  +---+---+---+---+---+---+---+---+---+---+---+---+---+---+ 


    //print out your array to see it's contents: 
    for (int i = 0; i < MAX; i++){ 
     cout << beadArray[i] << " "; 
    } 

    return EXIT_SUCCESS; 
} 
Смежные вопросы