我写了这段代码,但我没有用它。 [英] I wrote this code but I didnot work it.

查看:82
本文介绍了我写了这段代码,但我没有用它。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Create an array A and initialize it as: [13,8,17,11,14,63]
Declare an integer N and initialize it with -3.
Pass the array A and the variable N to a function like void func (int* A, int* N)
(N will be passed as a pointer. Your function will use the adress of N to reach the value -3.)
Inside the function, multiply N with every the elements of A.
Print the results of A.
Result should be like:
A a r ra y a t b egi n ni ng
13 8 17 11 14 63
A a r ra y a f t e r c a l l i n g f u n c ti o n
−39 −24 −51 −33 −42 −189




我的尝试:





What I have tried:

#include <stdio.h>


int multiply (int *N,int A[]);

main()
{
	int i,*N,n;
	int A[6]={13,8,17,11,14,63};
	n=-3;
	*N=&n;

	
	
	
	
		printf("A array at the beginning:\n ");
		for(i=0;i<6;i++)
		{
		printf("%d\t",A[i]);	
		}
	
		
		printf("\n A  array after calling function: \n");
		
		multiply( *N,A);
	
	return 0;
}
int multiply (int *N,int A[])
{
	int i,n;
	A[6] = 13,8,17,11,14,63;
	n=-3;
	*N=&n;


	for(i=0;i<6;i++)
	{
			printf("%d\t",A[i]*N);
	}
	
	return 0;
	
}

推荐答案

您永远不会将结果存储在数组中。也许该代码有帮助:

you never store the result in the Array. Maybe that code helps:
int multiply (int *N,int A[])
{
	int i;
	A[6] = 13,8,17,11,14,63;
 
	for(i=0;i<6;i++)
	{
            printf("%d\t",A[i]*N[i]);//only multiply
           N[i] = N[i]*A[i];//access each member AND not the first and set the result
            printf("%d\t",N[i]);
	}
	
	return 0;
}


当你不理解你的代码在做什么或为什么它做它的作用时,答案是 debugger

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量,这是一个令人难以置信的学习工具。



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



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

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

调试器在这里显示你的代码在做什么你的任务是与它应该做的事情进行比较。

调试器中没有魔法,它没有发现错误,它只是帮助你。如果代码没有达到预期的效果,那么您就接近了一个错误。
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, it is an incredible learning tool.

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.


#include <stdio.h>


int multiply (int* N,int A[]);

main()
{
	int i,*N,n;
	int A[6]={13,8,17,11,14,63};
	n=-3;
	N=&n;

	
	
	
	
		printf("A array at the beginning:\n ");
		for(i=0;i<6;i++)
		{
		printf("%d\t",A[i]);	
		}
	
		
		printf("\n A  array after calling function: \n");
		
		multiply( N,A);
	
	return 0;
}
int multiply (int* N,int A[])
{
	int i,n;
	A[6] = 13,8,17,11,14,63;
	n=-3;
	N=&n;


	for(i=0;i<6;i++)
	{
			printf("%d\t",*N*A[i]);
	}
	
	return 0; // and it works !!! thanks !!
	
}


这篇关于我写了这段代码,但我没有用它。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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