我的代码出了什么问题? [英] What's the wrong with my code ?

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

问题描述

嗨..有人可以告诉我这段代码有什么问题吗?



Hi.. Can someone please tell me what's the wrong with this code ?

int getSum(int*, int, int);

void main(void)
{
	int myArray[2][5]{{ 10, 10, 10, 10, 10 }, { 2, 2, 2, 2, 2 }};
	cout << getSum(*myArray, 2, 5) << endl;
}

int getSum(int* arr, int firstLength, int seconLength)
{
	int sum{};

	for (int i{}; i < firstLength; i++)
		for (int j{}; j < seconLength; j++)
			sum += *((arr+i)+j);
	return sum;
}





我想做的就是使用指针得到这个二维数组的摘要..但是这个函数返回92即使正确的答案是60.



请不要说使用arr [i] [j]表达..我想这样做指针..

提前致谢!



All I want to do is get the summary of this two dimensional array using pointers.. But this function returns 92 even if the correct answer is 60.

And please just don't say to use arr[i][j] expression.. I want to do this with pointers..
Thanks in advance !

推荐答案

好吧,想想你在做什么。

A 3 x 2数组将索引为:

Well, think about what you are doing.
A 3 x 2 array would be indexed as:
0,0   0,1   0,2
1,0   1,1   1,2

并且作为单个连续的块内存将是:

and as a single contiguous block memory would be:

 0     1     2     3     4     5
0,0   0,1   0,2   1,0   1,1   1,2



因此,当您使用索引添加到块起始地址时,不能只是将索引加在一起:


So when you use the indexes to add to the block start address, you can't just add the indexes together:

sum += *((arr+i)+j);

因为那永远不会访问最后两个元素!

相反,您需要将其中一个索引乘以行中元素的数量:

because that will never access the last two elements!
Instead, you need to multiply one of the indexes by the number of elements in the row:

sum += *(arr + (i * 3) + j);

(对于你的数组,它不会是3方式 - 但我非常有信心你可以解决它应该是什么!)

(For your array, it won't be "3" by the way - but I'm very confident you can work out what it should be!)


你的代码中有一些奇怪的语法;它甚至不会用Microsoft的C编译器编译。我终于得到了以下内容:

Some strange syntax in your code; it would not even compile with Microsoft's C compiler. I finally got it to work with the following:
int getSum(int* arr, int firstLength, int seconLength)
{
    int sum = 0;
    
    for (int i = 0; i < firstLength; i++)
        for (int j = 0; j < seconLength; j++)
            sum += *(arr+i*seconLength+j);
    return sum;
}

int main()
{
    int myArray[2][5] = {{ 10, 10, 10, 10, 10 }, { 2, 2, 2, 2, 2 }};
    cout << getSum((int*)myArray, 2, 5) << endl;
    
}


请查看此代码并了解您的错误



please check this code and you anderstand the your wrong

#include "stdafx.h"
#include <iostream>

using namespace std;


int getSum(int*, int, int);
 
void main(void)
{
	int myArray[2][5]={{ 10, 10, 10, 10, 10 }, { 2, 2, 2, 2, 2 }};
			cout<<"before sent to func"<<endl;
	for (int i=0; i<2;i++)
		for(int j=0;j<5;j++)
		cout<<"myArray["<<i<<"]["<<j<<"]="<<myArray[i][j]<<endl;
	cout <<"SUM="<< getSum(*myArray, 2, 5) << endl;
}
 
int getSum(int* arr, int firstLength, int seconLength)
{
	int sum=0;

 	cout<<"after sent to func"<<endl;
	for (int i=0; i < firstLength; i++)
		for (int j=0; j < seconLength; j++)
		{
			cout<<"myArray["<<i<<"]["<<j<<"]="<<*((arr+i)+j)<<endl;
			sum += *((arr+i)+j);
		}
	return sum;
}







发送到func之前



myArray [0] [0] = 10

myArray [0] [1] = 10

myArray [0] [2] = 10

myArray [0] [3] = 10

myArray [0] [4] = 10

myArray [1] [0] = 2

myArray [1] [1] = 2

myArray [1] [2] = 2

myArray [1] [3] = 2

myArray [1] [4] = 2





发送SUM的元素



myArray [0] [0] = 10

myArray [0] [1] = 10

myArray [0 ] [2] = 10

myArray [0] [3] = 10

myArray [0] [4] = 10

myArray [ 1] [0] = 10

myArray [1] [1] = 10

myArray [1] [2] = 10

myArray [1] [3] = 10

myArray [1] [4] = 2

SUM = 92




before sent to func

myArray[0][0]=10
myArray[0][1]=10
myArray[0][2]=10
myArray[0][3]=10
myArray[0][4]=10
myArray[1][0]=2
myArray[1][1]=2
myArray[1][2]=2
myArray[1][3]=2
myArray[1][4]=2


the elements to sent SUM

myArray[0][0]=10
myArray[0][1]=10
myArray[0][2]=10
myArray[0][3]=10
myArray[0][4]=10
myArray[1][0]=10
myArray[1][1]=10
myArray[1][2]=10
myArray[1][3]=10
myArray[1][4]=2
SUM=92


这篇关于我的代码出了什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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