我得到了所有相同的答案和他们中的许多人。 [英] I get all the same answers and to many of them.

查看:91
本文介绍了我得到了所有相同的答案和他们中的许多人。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了所有相同的答案和其中许多答案。

我有这个数据文件。

5

5 3 2 3 1 2

3 6 2 4

4 2 2 1 2

3 3 3

2 3 4



第一行是人数

第二行,第一行是一个人做运动的次数(其他数字)在同一行)

剩下的行与第二行相同



我必须写出每行多少练习是的。



我的计划:



我尝试过的:



我试图重新思考一切,但我没有看到问题。可能是我很糟糕。



我尝试过:



I get all the same answers and to many of them.
I have this data file.
5
5 3 2 3 1 2
3 6 2 4
4 2 2 1 2
3 3 3 3
2 3 4

The first line is the number of people
The second line, first number is how many times a person did an exercise(other numbers in the same line)
The rest of the lines are the same like the second line

I have to write out how many excercises each person did.

My program:

What I have tried:

I tried to rethink everything and I don't see the problem. Might be that I'm just bad.

What I have tried:

#include <cmath>
#include <fstream>
#include <iostream>
#include <iomanip>

using namespace std;

const char CDfv[]="duom.txt";
const char CRfv[]="rez.txt";

void writing(int n, int A[], int n2);

int A[100];

int main()
{
    int n, n2;
    writing(n,A,n2);
}

void writing(int n, int A[], int n2)
{
    ifstream fd(CDfv);
    ofstream fr(CRfv);
    fd>>n;
    int sum=0, a;

    for(int i=0;i<n;i++)
 {
="" fd="">>n2;
        for(int i2=0;i2<n2;i2++)
 {
="" fd="">>a;
            sum+=a;
        }
        fr<<sum<<endl;
 sum="0;
" }
="" fd.close();
="" fr.close();
}

<b="">

推荐答案

首先,你需要回到基础。

C按值传递所有函数参数 - 这意味着变量的副本被赋予函数,而不是它的引用。

所以当你这样做时:

First off, you need to go back to basics.
C passes all function parameters by value - which means that a copy of the variable is given to the function, not a "reference" to it.
So when you do this:
int main()
    {
    int n, n2;
    writing(n,A,n2);
    }

您将未初始化的值传递给写入函数,如果函数要更改它们,则无法获取任何值。 />
在函数内部忽略任何值,并将它们视为局部变量!



你需要做的是仔细考虑一下。 br />
首先从文件中读取第一行以获得后面的行数。

所以在循环中使用它来读取每一行 - 你不需要存储每行的数据,因此创建 A 数组没有意义。

对于每一行,读取一个整数:这是他们做的不同练习的数量。

现在在第一个循环中添加第二个循环,使用您刚刚读取的数字作为循环计数。在循环中,添加从行中读取的每个数字。

循环后,打印总数。

转到外循环中的下一行:



You pass uninitialised values to the writing function, and cannot get any values back if the function was to change them.
Inside the function you ignore any values, and treat them as local variables!

What you need to do is think a little more carefully.
Start by reading the first line from the file to get the number of lines that follow.
So use that in a loop to read each line - you don't need to "store" the data for each line so there is no point in creating your A array.
For each line, read an integer: this is the number of different exercises they did.
Now add a second loop inside the first, using the number you just read as teh loop count. Inside the loop, add each number you read from the line.
After the loop, print the total.
Move on to the next line in the outer loop:

lines = read count from file
loop 1: lineNo = 1 to lines inclusive incrementing by 1
   exercises = read count from file
   total = 0
   loop 2: exerciseCount = 1 to Exercises inclusive incrementing by 1
      iterations = read count from file
      total += itterations
   end loop 2
   print total
end loop 1

但是......这是你的作业,所以我不会给你任何代码!

But ... this is your homework, so I'll give you no code!


这篇关于我得到了所有相同的答案和他们中的许多人。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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