请在这个编码中需要帮助。我是C的新人 [英] Please,need help in this coding. I am new in C

查看:118
本文介绍了请在这个编码中需要帮助。我是C的新人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C编程的新手。请帮我解决这个问题。我的编码有很多错误。

问题:



编写一个程序来计算两个整数表中元素的总和。您的程序应该从键盘接收输入值并打印每行和每列中的所有值,最后显示总和值表。



输出

多少行? 2

多少列? 3

第一张表格:

输入行号的数据。 1

1 2 3

输入行号的数据。 2

4 5 6

第二张表格:

输入行号的数据。 1

7 8 9

输入行号的数据。 2

10 11 12

元素总和:

8 10 12

14 16 18


我的编码:

I am new in C programming. Pls help me solve this. my coding have alots of error.
the question:

Write a program to calculate the sum of elements in two tables of integers. Your program should receive input values from the keyboard and print all the values in each row and column and finally display a table of the summation value.

OUTPUT
How many rows? 2
How many columns? 3
First table:
Enter data for row no. 1
1 2 3
Enter data for row no. 2
4 5 6
Second table:
Enter data for row no. 1
7 8 9
Enter data for row no. 2
10 11 12
Sums of the elements:
8 10 12
14 16 18

My Coding:

#include<stdio.h>

void printArray( const int a[][] );

int main(void)
...{
int rows,column;
int a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r;

printf("How many rows?:\n");
scanf("%d",&rows);
printf("How many columns?\n");
scanf("%d",&column);

printf("First table: \n");
printf("Enter data for row no.1:\n");
scanf("%d,d,d",&a,b,c);
printf("Enter data for row no.2:\n");
scanf("%d,d,d",&d,e,f);

printf("Second table: \n");
printf("Enter data for row no.1:\n");
scanf("%d,d,d",&g,h,i);
printf("Enter data for row no.2:\n");
scanf("%d,d,d",&j,k,l);

m=a+g
n=b+h
o=c+i
p=d+j
q=e+k
r=f+l

int array1[ rows ][ column ] = { a,b,c,d,e,f };
int array2[ rows ][ column ] = { g,h,i,j,k,l };
int arraysum[ rows ][ column ] = { m,n,o,p,q,r, };

printf("Sum of the elements: \n");
printf("m,n,o"\n);
printf("p,q,r\n");

}

推荐答案

首先,您需要了解循环。它们可能在你的讲义中,但这里有一个简短的回顾:

1)声明一个变量来控制循环

First off, you need to learn about loops. They are probably in your lecture notes, but here is a brief recap:
1) Declare a variable to control the loop
int i;

2)设置一个循环:

2) Set up a loop:

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

这说:我从零开始。一直绕过循环,而i小于变量rows,每次绕循环时将一个加到i。

这意味着如果行是(说)3;它将围绕循环3次:当为零时,当我是1时,当我是2时。

3)在循环体中放入一些代码:

This says: "i" starts at zero. Go around the loop all the time while "i" is less than the variable "rows", adding one to "i" each time you have been round the loop.
That means that if rows is (say) 3; it will go round the loop 3 times: when is zero, when i is one, and when i is two.
3) Put some code in the loop body:

{
//Code here
}

这是每次循环时执行的代码。

现在,您可以从用户那里获得一行信息:

This is the code that will be executed each time it goes round the loop.
Now, you can get a row of information at a time from the user:

printf("How many rows?:\n");
scanf("%d",&rows);
printf("How many columns?\n");
scanf("%d",&columns);
int arrayOfData[100][100];
int x, y, currentValue;
for (y = 0; y < rows; y++)
   {
   printf("Enter data for row %d:\n", y);
   for (x = 0; x < columns; x++)
      {
      printf("   Enter value of column %d:", x + 1);
      scanf("%d", ¤tValue);
      arrayOfData[x][y] = currentValue;
      }
   }

一旦掌握了这一点,就可以看看使用循环来对行进行求和,等等。



免责声明:此代码未经测试,未编译,可能会出错 - 我没有可编程的C编译器......



(对于纯粹主义者来说,是的,它们是神奇的数字,没有范围检查:他刚刚开始,我们可以稍后介绍......他现在有更大的事情需要担心!)

Once you have got the hang of this, you can look at using loops to sum your rows, and so forth.

Disclaimer: This code is untested, uncompiled, and may give errors - I don't have a C compiler to hand...

(And for the purists, yes, they are magic numbers, no there are no range checks: He's just getting started, we can cover that later... He has much bigger things to worry about at the moment!)


http://wiki.answers.com/Q/Write_a_programme_for_addition_of_two_matrix_in_c_language [< a href =http://wiki.answers.com/Q/Write_a_programme_for_addition_of_two_matrix_in_c_languagetarget =_ blanktitle =New Window> ^ ]


这不是一个好的编程。

如果我输入10行和10列你会怎么做,你要定义100个变量吗?然后你打算如何处理这100个变量?

使用数组代替存储值。
This is not a good programming.
What you would do if i enter 10 rows and 10 columns, are you going to define 100 variables? and then how are you planning to handle those 100 variables?
Use arrays instead for storing values.


这篇关于请在这个编码中需要帮助。我是C的新人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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