第一个计划 [英] First Program

查看:66
本文介绍了第一个计划的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。今天我用C编写了我的第一个程序。它将元素加到了
数组中。我刚开始学习这门语言。关于更好的写/结构/格式/等方法的任何提示或指示。这段代码

将非常感激。感谢

MWT


的#include< stdio.h中>


INT add_array(INT ARR [ ],int arr_size)

{

int j;

int total = 0;

for(j = 0; j< arr_size; j ++)

总计+ = arr [j];

返回(总计);


}


main()

{

int size;


printf( 输入数组的大小>;;

scanf("%d",& size);

int array [size];

int i;

for(i = 0; i< size; i ++)

{

array [i] = i +1;


}


printf("数组元素的总和是%d。\ n",add_array(array,<登记/>
大小));

}

解决方案
" MWT"写道:

你好。今天我用C编写了我的第一个程序。它将数组中的元素加起来。我刚开始学习这门语言。有关更好的写/结构/格式/等方法的任何提示或指示。这段代码将非常感谢。感谢
MWT

的#include< stdio.h中>

INT add_array(INT ARR [],INT arr_size)
{<无线电通信/> int j;
int total = 0;
for(j = 0; j< arr_size; j ++)
total + = arr [j];
return(total );



主要()
{
int size;

printf("数组的输入大小> ;");
scanf("%d"& size);
int array [size];
int i;
for(i = 0; i< ; size; i ++)
{array = [i] = i + 1;

}
printf("数组元素之和为% d.\ n,add_array(数组,
大小));
}




假设它有效,我会说你是一个良好的开端。


" mwt" < MI ********* @ gmail.com>写道:

你好。今天我用C编写了我的第一个程序。它将数组中的元素加起来。我刚开始学习这门语言。有关更好的写/结构/格式/等方法的任何提示或指示。这段代码将非常感谢。 。
感谢
的#include< stdio.h中>

INT add_array(INT ARR [],INT arr_size)
{
诠释J;
int total = 0;
for(j = 0; j< arr_size; j ++)
total + = arr [j];
return(total);

}

main()
{
int size;

printf(" array size of gt;");
scanf("%d",& size);
int array [size];


^ ---看起来像gcc功能所以可能不便携

(这是在C99?)。


你应该使用:


int * array = malloc(size * sizeof(* array))

int i;
for(i = 0; i< size; i ++)
{
array [i] = i + 1;
}

printf("数组之和)元素是%d。\ n",add_array(array,
size));


,当你完成它时不要忘记释放它:


免费(阵列)

}




除此之外看起来还不错。风格很好AFAICT。


-

burton samograd kruhft .at。 Gmail的

kruhft.blogspot.com www.myspace.com/kruhft metashell.blogspot.com


> ^ ---看起来像一个gcc功能,所以它可能不便携

(是这是在C99?)。


我用gcc编译。除此之外,我不知道它是什么类型的C



为了学习最大的便携功能,基于linux的编译器(或
$ b)我应该使用$ b bcc上的标志?


Hello. Today I wrote my first program in C. It adds up the elements in
an array. I am just beginning to learn this language. Any tips or
pointers about better ways to write/structure/format/etc. this code
would be much appreciated. Thanks.
mwt.

#include <stdio.h>

int add_array(int arr[], int arr_size)
{
int j;
int total = 0;
for(j = 0; j < arr_size; j++)
total += arr[j];
return(total);

}

main()
{
int size;

printf("Input size of array>");
scanf("%d", &size);
int array[size];
int i;
for(i=0; i<size; i++)
{
array[i] = i+1;

}

printf("Sum of array elements is %d.\n", add_array(array,
size));
}

解决方案

"mwt" writes:

Hello. Today I wrote my first program in C. It adds up the elements in
an array. I am just beginning to learn this language. Any tips or
pointers about better ways to write/structure/format/etc. this code
would be much appreciated. Thanks.
mwt.

#include <stdio.h>

int add_array(int arr[], int arr_size)
{
int j;
int total = 0;
for(j = 0; j < arr_size; j++)
total += arr[j];
return(total);

}

main()
{
int size;

printf("Input size of array>");
scanf("%d", &size);
int array[size];
int i;
for(i=0; i<size; i++)
{
array[i] = i+1;

}

printf("Sum of array elements is %d.\n", add_array(array,
size));
}



Assuming that it works, I would say you are off to a good start.


"mwt" <mi*********@gmail.com> writes:

Hello. Today I wrote my first program in C. It adds up the elements in
an array. I am just beginning to learn this language. Any tips or
pointers about better ways to write/structure/format/etc. this code
would be much appreciated. Thanks.

#include <stdio.h>

int add_array(int arr[], int arr_size)
{
int j;
int total = 0;
for(j = 0; j < arr_size; j++)
total += arr[j];
return(total);

}

main()
{
int size;

printf("Input size of array>");
scanf("%d", &size);
int array[size];
^--- looks like a gcc feature so it might not be portable
(is this in C99?).

you should use:

int *array = malloc(size*sizeof(*array))
int i;
for(i=0; i<size; i++)
{
array[i] = i+1;
}

printf("Sum of array elements is %d.\n", add_array(array,
size));
and don''t forget to free it when you''re finished with it:

free(array)
}



Other than that it looks good. Style is fine AFAICT.

--
burton samograd kruhft .at. gmail
kruhft.blogspot.com www.myspace.com/kruhft metashell.blogspot.com


>^--- looks like a gcc feature so it might not be portable
(is this in C99?).

I am compiling with gcc. Other than that, I have no idea what type of C
it is.
For learning maximally portable features, what linux-based compiler (or
perhaps flags on gcc) should I be using?


这篇关于第一个计划的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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