422,023 Members | 1,030 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 422,023 IT Pros & Developers. It's quick & easy.

Detect all the possible errors in C++ code

P: 2
1. #include <iostream>
2. using namespace std;

3. void generateTable(int x, int y){
4. int result = y / x;

5. for(int i = 1; i <= 10; i++){
6. cout<<result<<" * "<<i<<" = "<<i*result<<endl;
7. }
8. }

9. main(){
10. int found;
11. int data[10];
12. for(int i = 0; i < 10; i++){
13. for(int j=0; j < 10; j++){
14. found++;
15. }
16. }
17. cout<<"Loops executed "<<found<<" times"<<endl;
18. for(int i = 0; i<= 10; i++){
19. data[i] = 0;
20. }

21. int first, second;

22. cout<<"Enter first number: ";
23. cin>>first;

24. cout<<"Enter second number: ";
25. cin>>second;

26. cout<<"Subtract first number by second and show math table of resut"<<endl;

27. generateTable(first, second);

28. int rem = first%2;

29. if(rem = 0){
30. cout<<"First number is even"<<endl;
31. }
32. else{
33. cout<<"First number is odd"<<endl;
34. }
35. }
Nov 29 '17 #1
Share this Question
Share on Google+
3 Replies


P: 2
Coupling refers to how many dependencies there are between modules and the nature of the links. Based on the nature of dependency, there are different type of coupling like data coupling, stamp coupling, content coupling and control coupling etc. You are required to identify the type of coupling that the following statements represent. Justify your answer with solid reason.
i. Module A ``branches'' or ``falls through'' into Module B (by containing a GOTO statement that transfers control somewhere into the middle of Module B).
ii. Module A refers to, or changes, Module B's internal (and, again, ``private'') data
iii. Module A changes one of the statements in Module B's object code.
Nov 29 '17 #2

P: 2
You forgot to add .h at the end of upstream
Check the equality operator at line 29
Dec 1 '17 #3

Expert 100+
P: 2,357
  1. Lines 12-16: don’t see what this is for; what do you intend this code to do?
  2. Line 14: found hasn’t been initialized.
  3. Lines 18-20: last pass will access nonexistent location data[10].
  4. Lines 18-20: don’t see what this is for (data array never used again); what do you intend this code to do?
  5. Line 26: “Divide second number by first” instead of “Subtract first number by second”; misspelled “result”.
  6. Line 29: as noted by RGauthamRam.
  7. General: what do you want to happen when the first number (the divisor) is zero?
  8. General: do you want generateTable to always divide second number by first; or would it be more helpful to divide the larger magnitude number by the smaller magnitude number (-5 has larger magnitude than +2) so that result is never 0?
Dec 2 '17 #4

Post your reply

Sign in to post your reply or Sign up for a free account.