现在我改进了我的代码,但.... [英] Now I improve my code but ....

查看:58
本文介绍了现在我改进了我的代码,但....的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做4x4矩阵乘法运行完美。现在我尝试添加加法运算,但我得到这个[错误]无效类型'int [int]'对于数组下标

这是错误的。



我尝试过:



i do 4x4 matrix multiplication it runs perfectly. now i try to add Addition operation but i get this " [Error] invalid types 'int[int]' for array subscript""
whats wrong it this.

What I have tried:

#include<iostream>
#include<conio.h>
using namespace std;

class result {
	public:
		int mlt[4][4];
		int i,j;
		int add[4][4];		
	void multipication_matrices(int a[4][4],int b[4][4])
{
 	int i,j,k,mult;

	for(i = 0; i < 4; ++i)
	{
		 for(j = 0; j < 4; ++j)
        {
        	 for(k = 0; k < 4; ++k)
            {
                mlt[i][j] += a[i][k] * b[k][j];
            }
		}          
		
	}
	        /* YAZDIRMA İŞLEMİ */ 
  	for(i = 0; i < 4;i++)
	{
	   for(j = 0; j < 4;j++) 
	     {
			 cout << " " << mlt[i][j];
			  if(j == 4-1)
		       cout << endl;
		  }
	}

}

	void addition_matrices(int a[4][4],int b[4][4])
	{
		int i,j,add;
		
		for(i=0; i<4; i++){
		
        for(j=0; j<4; j++)
		{
	   
       		add[i][j] = a[i][j] + b[i][j]; // mistake  [Error] invalid types 'int[int]' for array subscript
        }}
		for(i=0; i<4; i++)
       {
       for(j=0; j<4; j++)
       cout<<add[i][j]<<"\t";  // mistake  [Error] invalid types 'int[int]' for array subscript
       cout<<" ";52	19		 
      }
       cout<<" ";
	}

}r;


int main()
{
 	int i,j,k,mult;
 	int array1[4][4],array2[4][4],mlt[4][4] = {0};
	
	
		cout << "Enter elements of matrix 1:" << endl;
       for(i = 0; i < 4; ++i)
        for(j = 0; j < 4; ++j)
        {
            cout << "Enter element a" << i + 1 << j + 1 << " : ";
            cin >> array1[i][j];
        }

    // Storing elements of second matrix.
    cout << "Enter elements of matrix 2:" << endl;
    for(i = 0; i < 4; ++i)
        for(j = 0; j < 4; ++j)
        {
            cout << "Enter element b" << i + 1 << j + 1 << " : ";
            cin >> array2[i][j];
        }
        
        
    r.multipication_matrices(array1,array2);
	r.addition_matrices(array1,array2);
        
	return 0;
}

推荐答案

Quote:

int add [4] [4];

int add[4][4];

你有这个类成员来存储添加的结果。



然而你宣布

You have this class member in order to store the result of the addition.

However you are declaring

引用:

int i,j, add ;

int i,j,add;

在加法方法中。本地(幸运的是错误的)声明隐藏了外部声明。

inside the addition method. The local (luckily wrong) declaration hides the external one.


引用:

代码只记忆第一排和冒号。因此,输出总是相同的数字。

1.row X 1.冒号其他行和冒号不计算。

the code just take memory a firt row and colon. so, output always be same number.
"1.row X 1. colon" the other rows and colons dont calculete.



你的代码输出最后一个row,乘法的最后一列,因为它是您请求的。并且你反复计算与输出相同的东西。

使用调试器查看你的代码在做什么。



有一个工具可以允许您查看代码正在执行的操作,其名称为调试程序。它也是一个很好的学习工具,因为它向你展示了现实,你可以看到哪种期望与现实相符。

当你不明白你的代码在做什么或为什么它做它做的时候,答案就是答案是调试器

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]



掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

调试器在这里向您展示您的代码正在做什么,您的任务是与什么进行比较应该这样做。

调试器中没有魔法,它没有找到错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。


Your code output the last row, last column of multiplication because it is what you requested. and you repeatedly calculate the same thing as output.
Use the debugger to see what your code is doing.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于现在我改进了我的代码,但....的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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