帮我写程序c ++ [英] help me to write program c++

查看:104
本文介绍了帮我写程序c ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用c ++编写程序以使用完整的加法器计算并打印用户的两个5位二进制输入的总和并进位?

这是我拥有的代码:

[代表OP从非解决方案插入] -MRB

write program in c++ to compute and print the sum and carry of two 5 bit binary input from user using full adder ?

This is the code I have sofar:

[Inserted from non-solution on OP''s behalf] -MRB

#include <iostream.h>
using namespace std;
 
void  main()
{
      int ar[5];
      int ar1[5];
      cout<<"Enter the First Binary number: "<<endl;
      for(int i=0;i<5;i++)
          cin>> ar[5];
      cout<<"Enter the Second Binary number: "<<endl;
      for(int j=0;i<5;j++)
        cin>> ar1[5];
     if(ar[i]+ar1[j]==0)
        cout<<"sum =0 ";
     if(ar[i]+ar1[j]==1)
       cout<< "sum= 1" ;
     if(ar[i]+ar1[j]==2)
       cout<<"sum=0 ";
    if(ar[i]+ar1[j]==3)
    cout<<" sum=1 ";
}



谁能告诉我这段代码有什么问题吗?

在此先感谢您!



Can anybody tell me what''s wrong with this code?

Thanks in advance!

推荐答案

这里没人会帮您做作业.这不是因为我们觉得我们比您强,还是某种程度上是精通的编码人员,而是因为您不会学到任何东西.您的讲师应该已经为您提供了完成任务所需的所有指导.
Nobody here is going to do your homework for you. It''s not because we feel like we''re better than you, or are somehow elite coders - it''s because you won''t learn anything. Your instructor should have given you all of the guidance you need to complete the task.


尝试以非编程的方式思考问题,并在纸上写下完成此任务所需的步骤.然后,您可以按照这些步骤进行操作,以检查逻辑是否正确,然后再转换为实际的C ++代码.
Try thinking about the problem in non-programming terms and write on paper the steps needed to accomplish this. You can then work through those steps to check that your logic is correct before converting to actual C++ code.


1.第一个输入循环使用了错误的索引
1. The first input loop uses the wrong index
cin>> arr[5]
// here   ^ 



2.在第二个循环头中输入:



2. Typo in second loop header:

for(int j=0;i<5;j++)
// here     ^



3.一行之后,出现与1..br中描述的错误相同的错误.
4.输入后的整个代码块使用索引值ij引用单个数组元素,但是这些值从未更改-您在此处缺少循环.

考虑一下半加法器的功能,然后称呼它.与其将半加法器的代码写到main()中,不编写一个单独的函数,然后从main调用它-请记住,由于要处理5位数字,因此您至少需要调用该半加器5次.

我认为上面的大部分内容应该已经在您的说明中,或者在老师根据其授课的任何书中.在此处查看更多详细信息.



3. One line later, same error as described in 1.

4. The whole code block after the input refers to single array elements using the index values i and j, but these values are never changed - you are missing a loop here.

Think about what a half adder does, and then how you call it. Rather than write the code for the half adder into main(), write a separate function, and call that from main - remember that as you have 5 digits to deal with, you need to call that half adder at least 5 times.

I''d think that most of the above paragraph should be in your instructions already, or in whatever book the instructor is basing the lessons on. Look there for more details.


这篇关于帮我写程序c ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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