如何从用户(即控制台)馈送数组元素以及如何将数组元素传递到函数作为参数 [英] How to feed array elements from user (i.e., the console) and how to pass array elements to a function as arguments

查看:116
本文介绍了如何从用户(即控制台)馈送数组元素以及如何将数组元素传递到函数作为参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是Visual C ++的原始代码,我一直试图操纵一天以上。
这是最简单的无错误程序,可以正确运行。
我想做的是插入两个函数:一个用于从用户输入,另一个用于显示输出。

Here is the original code of Visual C++ which I have been trying to manipulate for more than a day. This is the simplest error-free program which runs correctly. What I want to do is insert two functions: one for taking input from a user, and another for displaying output.

#include<iostream>
#include<iomanip>
using namespace std;

void main()
{
    int i, j,r,c;
    int arr[5][5];
    cout<< "enter r and c";
    cin>> i>> j;

    for(r=1;r<=i;r++)
    {
        for(c=1;c<=j;c++)
        {
            cout<< "enter elements";
            cin>> arr[r][c];
        }
    }

    cout<< " elements are"<< endl;
    for(r=1;r<=i;r++)
    {
        for(c=1;c<=j;c++)
        {

            cout<<setw(4) <<arr[r][c];
        }
        cout<< endl<< endl;
    }

    cin.ignore();
    getchar();
}

这是我试着做的,但它会产生错误。

This is what I tried doing but it's giving errors.

#include<iostream>
#include<iomanip>
using namespace std;
int input(int i, int j, int arr)
{   
    int r,c;
    for(r=1;r<=i;r++)
    {
        for(c=1;c<=j;c++)
        {
            cout<< "enter elements";
            cin>> arr[r][c];

        }
    }
    return arr[r][c];
}
void output(int i, int j, int arr)
{   
    int r,c;
    for(r=1;r<=i;r++)
    {
        for(c=1;c<=j;c++)
        {

            cout<<setw(4) <<arr[r][c];
        }
        cout<< endl<< endl;
    }
}

void main()
{
    int i, j,r,c;
    int arr[5][5];
    cout<< "enter r and c";
    cin>> i>> j;

    input(i,j,arr);

    cout<< " elements are"<< endl;

    output(i,j,arr);
    cin.ignore();
    getchar();
}


推荐答案

的C ++,指针和函数。下面是你正在寻找的(排序),但是,你肯定需要更深入地了解C ++:

You really need a deeper understanding of C++, pointers, and functions. The following is what you are looking for (of sort), however, you certainly need a much deeper understanding of C++:

#include<iostream>

using namespace std;

void output(int ** array)
{
    for (int i= 0;i<5;++i)
    {
        for (int a= 0;a<5;++a)
        {
            cout << "Array[" << i << "][" << a << "] = " << array[a][i] << endl;
        }
    }
}

void input(int ** array)
{
    for (int i= 0;i<5;++i)
    {
        for (int a= 0;a<5;++a)
        {
            cout << "Array[" << i << "][" << a << "] = " << endl;
            cin >> array[i][a];
        }
    }
}

int main()
{
    int** array;
    array = new int*[5];
    for (int i=0;i<5;++i)
        array[i] = new int[5];
    input(array);
    output (array);
    delete []array;
    cin.ignore();
    getchar();
    return 0;
}

问题是,解释这将像从头开始教你C ++。无论你使用什么方法来学习C ++,坚持下去。你会到那里,你只是还没有准备好。你会发现一件事并不总是容易把事情提高到一个新的水平。有一个原因,事情去,停止在学习时他们做什么。

The problem is, explaining this would be like teaching you C++ from scratch. Whatever method you are using to learn C++, stick with it. You will get there, you're just not quite ready. One thing you will find is it's not always easy to take things to the next level. There is a reason things go and stop where they do when learning. Though, taking that extra step, or attempting to, isn't bad, you just need to know when it isn't time, yet.

编辑

为了完成起见,下面是一个没有指针的例子:

For the sake of completion, here is an example with no pointers:

#include<iostream>

using namespace std;

void output(int array[5][5])
{
    for (int i= 0;i<5;++i)
    {
        for (int a= 0;a<5;++a)
        {
            cout << "Array[" << i << "][" << a << "] = " << array[a][i] << endl;
        }
    }
}

void input(int array[5][5])
{
    for (int i= 0;i<5;++i)
    {
        for (int a= 0;a<5;++a)
        {
            array[i][a] = i*a;
        }
    }
}

int main()
{
    int array[5][5];
    input(array);
    output (array);
    cin.ignore();
    getchar();
    return 0;
}

这篇关于如何从用户(即控制台)馈送数组元素以及如何将数组元素传递到函数作为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆