简体   繁体   English

使用循环填充的数组* AND交换

[英]Array that fills using a loop *AND swaps

I did this program in class and I'm trying to recreate it for an exam coming up. 我在课堂上做了这个程序,并且试图为即将来临的考试重新创建它。 The program is supposed to be an array[2][10] and is supposed to output numbers in this order: 该程序应为array [2] [10],并应按以下顺序输出数字:

        1 3 5 7 9 11 13 15 17 19
        0 2 4 6 8 10 12 14 16 18

I'm really lost on this and I could really use any help. 我真的迷失了,我真的可以使用任何帮助。

#include<iostream>
#include <string>
#include <cstring>
using namespace std;


void out(int n[2][10]);
void fillit(int n[2][10]);
int main(){
int nums[2][10];
fillit(nums);
out(nums);
}
void fillit(int n[2][10]){
n[0][0] = 1;
n[1][0] = 0;

for (int i = 1; i < 10; i++){
    n[0][i] = n[0][i] + 2;
    n[1][i] = n[0][i] + 2;
}
}
void out(int n[2][10]){
for (int r = 0; r <= 1; r++){
    for (int c = 0; c <= 9; c++){
        cout << n[r][c];
    }
    cout << endl;
}
}

Update I have the program successfully filling the array but now I need the program to swap row 1 with row 2 and then output the new array. 更新我有程序成功填充数组,但是现在我需要程序将第1行与第2行交换,然后输出新数组。 Ie 0 2 4 6 8 10 12 14 16 18 1 3 5 7 9 11 13 15 17 19 即0 2 4 6 8 10 12 14 16 18 1 3 5 7 9 11 13 15 17 19

void fillit(int n[2][10]){

 for (int i = 0; i < 10; i++){
    n[0][i] = (i * 2 ) + 1;
    n[1][i] = i * 2;
 }
}
#include<iostream>
#include <string>
#include <cstring>
using namespace std;


void out(int n[2][10]);
void fillit(int n[2][10]);
int main(){
int nums[2][10];
fillit(nums);
out(nums);
}
void fillit(int n[2][10]){
n[0][0] = 1;
n[1][0] = 0;

for (int i = 1; i < 10; i++){
    n[0][i] = n[0][i-1] + 2;
    n[1][i] = n[0][i-1] + 2;
}
}
void out(int n[2][10]){
for (int r = 0; r <= 1; r++){
    for (int c = 0; c <= 9; c++){
        cout << n[r][c];
    }
    cout << endl;
}
}

How about this, its shorter? 怎么样,它更短?

int nums[2][10];
for (int i = 0; i < 20; i++)
{
    nums[(i%2)^1][i/2] = i;
}

I noticed that second array elements are even numbers, and the first array corresponding elements are one bigger (and thus odd) ... so this answer accomplishes using ONLY addition. 我注意到第二个数组元素是偶数,而第一个数组对应的元素是一个更大的(因此是奇数)...因此,此答案使用加法完成。 Might be easier to understand. 可能更容易理解。

void fillit(int n[2][10])
{
    int even = 0; // start value

    for (size_t i = 0; i < 10; i++)
    {
       int odd  = even + 1;
       n[0][i] = odd;
       n[1][i] = even;  
       even += 2;
    }
}

I noticed that you tagged this problem with C++. 我注意到您用C ++标记了此问题。 Perhaps you should try vectors. 也许您应该尝试使用向量。

For small vectors, you simply declare with initial values in curly-braces, making it easy to define the matrix limits [2] and [10]. 对于小向量,您只需在大括号中声明初始值,即可轻松定义矩阵极限[2]和[10]。 In this example, I initialized using easy to recognize values, so you can tell the 2x10 matrix has not yet been filled. 在此示例中,我使用易于识别的值进行了初始化,因此您可以知道2x10矩阵尚未填充。 Without this init, the values contained will be random noise. 没有此初始化,包含的值将是随机噪声。

std::vector<std::vector<int>> n {
   {  11,  12,  13,  14,  15,  16,  17,  18,  19, 20 }, // row 1
   {  21,  22,  23,  24,  25,  26,  27,  28,  29, 30 }  // row 2
   //  1    2    3    4    5    6    7    8    9  10   <-- col
}; 

Yes, you could write in the target value of your output, but then fillit() would not be needed. 是的,您可以输入输出的目标值,但是不需要fillit()。

For passing the 2x10 vector to fillIt(), remember that you need to mark the matrix as a reference. 要将2x10向量传递给fillIt(),请记住您需要将矩阵标记为参考。 The following emphasizes that n[0] contains odd numbers, and n[1] contains 10 even numbers. 以下重点说明n [0]包含奇数,n [1]包含10个偶数。

//                vector x vector        v--reference
void fillIt(std::vector<std::vector<int>>& n)
{
   int even = 0;
   for (size_t c = 0; c < 10; ++c) // row major {
      int odd = even + 1;
      n[0][c] = odd;
      n[1][c] = even;
      even += 2;
   }
}

For test, I recommend just passing the 2x10 vector to a new function "showIt()". 为了进行测试,我建议仅将2x10向量传递给新函数“ showIt()”。

// do not modify--vvvvv                 do not copy--v
void vec2x10Show (const std::vector<std::vector<int>>& n)
{
   // header
   std::cout << "\nCOL->";
   for (int i=0; i<10; ++i) std::cout <<  std::setw(3) << i+1 << " ";

   std::cout << "\nr1:  "; //                              r  c
   for (int c=0; c<10; ++c) std::cout << std::setw(3) << n[0][c] << " ";

   std::cout << "\nr2:  "; //                              r  c
   for (int c=0; c<10; ++c) std::cout << std::setw(3) << n[1][c] << " ";

   std::cout << std::endl;
}

Note the hard coded '10' in each loop (a magic number). 注意每个循环中的硬编码“ 10”(一个魔术数字)。 Using vectors, this magic number is not necessary because the vector includes this information (your next assignment). 使用向量时,不需要这个幻数,因为向量包括此信息(您的下一个分配)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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