Advertisement
Guest User

Untitled

a guest
Dec 18th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.20 KB | None | 0 0
  1. Q9: What is the output of the following program:
  2. #include <iostream>
  3. using namespace std;
  4. int main() {
  5.        int a[] = { 6,1,6,3,1,4,5,6,4,7 };
  6.        for (int i = 0; i < 10; i++)
  7.        {
  8. }
  9. bool found = false;
  10. for (int j = i - 1; j >= 0; j--)
  11.        if (a[j] == a[i])
  12.               found = true;
  13. if (!found)
  14.        cout << a[i] << endl;
  15. return 0; }
  16. Q10: What is the output of the following program:
  17. #include <iostream>
  18. #include <fstream>
  19. using namespace std;
  20. int main()
  21. {
  22. ifstream inputFile;
  23. inputFile.open("numbers.txt");
  24. int v1 = 0, number;
  25. while (inputFile >> number)
  26.        v1 += number;
  27. cout<<v1 << endl;
  28. return 0; }
  29. The content of numbers.txt file is “1 2 3 4 5” Q11: What is the output of the following program:
  30.  #include <iostream>
  31.  using namespace std;
  32.  void function1(int n)
  33.  {
  34. while (n > 0) {
  35.               cout << n % 2;
  36.               n = n / 2;
  37.        }
  38.        cout << endl;
  39. }
  40. int main() {
  41.        function1(10);
  42. return 0; }
  43. . Q12: What is the output of the following program:
  44.    #include <iostream>
  45.    #include <fstream>
  46.    using namespace std;
  47.    int main()
  48. {
  49. ifstream inputFile;
  50. inputFile.open("numbers.txt");
  51. int n,t=0;
  52. for (int i = 0; i < 5; i++)
  53. {
  54.        inputFile >> n;
  55. t += n; }
  56. cout<< t/5.0 << endl;
  57. return 0; }
  58. The content of numbers.txt file is 1
  59. 2
  60. 3
  61. 4 5
  62. Q13: What is the output of the following program:
  63.    #include <iostream>
  64.    #include <iomanip>
  65.    using namespace std;
  66.    int main()
  67. {
  68. }
  69. struct Distance
  70. {
  71.        int Meters;
  72.        int Centimeters;
  73. };
  74. Distance d1 = {20,90};
  75. Distance d2 = { 10, 14 };
  76. Distance dResult;
  77. dResult.Centimeters = d1.Centimeters + d2.Centimeters;
  78. dResult.Meters = d1.Meters + d2.Meters;
  79. if (dResult.Centimeters >= 100)
  80. {
  81.        dResult.Centimeters -= 100;
  82.        dResult.Meters++;
  83. }
  84. cout << dResult.Meters << '.' << setw(2) << setfill('0')
  85.        << dResult.Centimeters << "." << endl;
  86. return 0;
  87. 8
  88.  
  89. Q14: What is the output of the following program:
  90. #include <iostream>
  91. using namespace std;
  92. int main()
  93. {
  94.    int a[] = { 2,6,2,3,6,4,5,6,4,7 };
  95.    for (int i = 0; i < 10; i++)
  96.    {
  97. }
  98. int count = 0;
  99. bool found = false;
  100. for (int j = 0; j < 10; j++)
  101.        if (a[j] == a[i])
  102.               if (j >= i)
  103. count++;
  104. else
  105. {
  106. found = true;
  107. break; }
  108. if (!found)
  109.        cout << "value: " << a[i] << "\tcount= " << count << endl;
  110. return 0; }
  111. . Q15: What is the output of the following program:
  112.       #include <iostream>
  113.       #include <string>
  114.       using namespace std;
  115.       struct student
  116. {
  117. int id;
  118. string name;
  119.          int grades[6];
  120.       };
  121.       double doSomething(int a[])
  122.       {
  123.          int t = a[0];
  124.          for (int i = 1; i < 6; i++)
  125.                 if (a[i] > t)
  126.                        t = a[i];
  127. return t; }
  128. int main() {
  129.          student s1 = { 101,"Mohamed",{90,80,70,95,85,88} };
  130.          student s2 = { 102,"Ahmed",{ 90,70,60,85,75,68 } };
  131.          student s3 = { 103,"Naer",{ 92,90,50,75,65,78 } };
  132.          cout << "ID\tName\tAverage" << endl;
  133. cout << s1.id << "\t" << s1.name << "\t" << doSomething(s1.grades) << endl; cout << s2.id << "\t" << s2.name << "\t" << doSomething(s2.grades) << endl; cout << s3.id << "\t" << s3.name << "\t" << doSomething(s3.grades) << endl; return 0;
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement