本文介绍了帮我写程序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!

推荐答案



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 ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 19:16