它是否正确?如果不是我哪里错了? [英] Is This Correct? If Not Where Did I Go Wrong?

查看:76
本文介绍了它是否正确?如果不是我哪里错了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

写一个带签名的C函数

void multiScalar(int A [6] [6],int factor);

将6x6矩阵A乘以标量因素



脚本是:

Write a C function with signature
void multiScalar(int A[6][6], int factor);
that multiplies a 6x6 matrix A by a scalar factor

Script is:

#include <stdio.h>
void multiScalar(int A[6][6], int factor);

{ 
A = [1 2 3 4 5 6; 1 2 3 4 5 6; 1 2 3 4 5 6; 1 2 3 4 5 6; 1 2 3 4 5 6; 1 2 3 4 5 6];
multiScalar= (A[6][6]*factor);
}

推荐答案

这不是错的,它是错的,我不知道如何帮助你。我的意思是,我会给你答案,但不确定它会对你有所帮助,因为你的问题不是这个问题,而是了解你在做什么。



所以,每一行都是错误的,包括函数签名。标量的一些矢量代数对象的标量乘法是一个非常普遍的事情;它不仅不依赖于维度(6,6),甚至不依赖于等级(类型中的'[]'的数量)。为什么?因为这样的产品只是保存非常数组,每个成员乘以 factor 。你正在使用的形式,这是一个排名2的锯齿状阵列;但它并不是真正的锯齿状,因为你想要6 x 6元素。因此,它可能是一维数组(我故意使用引号,因为这不是真正的数学维度)可以表示不同等级的数组。要理解它,您可以阅读: http://www.cplusplus.com/doc/tutorial/arrays [ ^ ]。



这是事情:乘以标量可以与数组的解释无关,你只需要使用你完全有N个元素的事实,应该知道如何访问每个元素,将它乘以因子。对于此任务,等级和维度无关紧要。你只是做一个非常简单和一般的任务太专业,这总是很糟糕。



即使你想要 int [] [] ,你可以传递数组对象,两个维度和因子



第二行, A = ... 完全没有意义,我甚至无法讨论它,因为很难理解你的想法。让我们忘记吧。同样的事情是下一行。 multiScalar 是您的函数的名称,那么如何为它分配任何数组?公平地说,我必须承认这种语法在其他一些(好的!)语言中使用,但这是C ++。这完全没有意义。



那么,该怎么办?在一个循环中,遍历数组的所有元素,并将每个成员乘以 factor 。循环及其参数(或嵌套循环)取决于函数的签名。你真的应该自己编写代码。使用我上面解释的想法,首先要确定你真正需要的东西,然后再做它。



-SA
This is not just wrong, it's so wrong that I have no idea how to help you. I mean, I'll give you the answer, but not sure that it will help you, because your problem is not this particular problem, but understanding of what you are doing.

So, every line is wrong, including the function signature. Scalar multiplication of some vector-algebra object by scalar is a very general thing; it does not depend not only on dimensions (6, 6), but even on rank (number of '[]' in your type). Why? Because such product is simply the save very array, with each of the members multiplied by factor. The form you are using, this is a "jagged array" of rank 2; but it's not really jagged, because you want 6 x 6 elements. So, it could be "1-dimensional" array (I use quotation marks on purpose, because this is not real mathematical "dimension") could represent arrays of different ranks. To understand it, you can read this: http://www.cplusplus.com/doc/tutorial/arrays[^].

Here is the thing: multiplication by scalar can be agnostic to the interpretation of the array, you only need to use the fact that you have N elements altogether, should know how to access each, to multiply it by factor. For this task, ranks and dimensions are irrelevant. You just make a very simple and general task too much specialized, which is always bad.

Even if you wanted int[][], you could pass the array object, two dimensions and factor.

Second line, A = ... makes no sense at all, I cannot even discuss it, because it's hard to understand your idea. Let's just forget it. Same thing is the next line. multiScalar is the name of your function, so how can you assign to it anything, an array or not? To be fair, I must admit that such syntax is uses in some other (good!) languages, but this is C++. It makes no sense at all.

So, what to do? In a cycle, traverse all elements of your array and multiply each member by factor. The cycle and its parameter (or nested cycles) depend on the signature of the function. You really should write the code yourself. Using the ideas I explain above, first decide what you really need and than do it.

—SA

我的建议:暂时忘记这个程序。



理解语言语法时遇到很大问题。

唯一的解决方案是阅读语言文档,尽可能多地阅读教程。



在你的3行代码中,我看到了9个失败原因。
My advice: forget this program for now.

You have a big problem understanding the language syntax.
The only solution is to read the language documentation, follow as many tutorials as you can.

In your 3 lines of code, I see 9 reasons of failure.


这篇关于它是否正确?如果不是我哪里错了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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