Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using a for loop structure, write a code fragment to find the sum of every odd e

ID: 3780055 • Letter: U

Question

Using a for loop structure, write a code fragment to find the sum of every odd element in an array called "some_ints" that has 100 elements. Begin at the beginning of the array and end with the last odd element (e.g. some_ints[1], some_ints[3], some_ints[5], ... some_ints[99]). Write the C++ statement(s) necessary to solve the common mathematical expression, x = -b plusminus squareroot b^2 - 4ac/2a Define a function called "round" that rounds a number to a certain number of decimal places. For example.

Explanation / Answer

// Question 26
// C++ code
#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
using namespace std;

int main()
{
   int some_ints[100];

   // filling array with random numbers
   for (int i = 0; i < 100; ++i)
   {
       some_ints[i] = rand() % 1000 + 1;
   }

   // initialize total sum
   int sum = 0;

   // add odd index values
   for (int i = 1; i < 100; i=i+2)
   {
       sum = sum + some_ints[i];
   }

   cout << "Sum: " << sum << endl;
   return 0;
}

// Question 27
/*
   double x1 = ((-1)*b + sqrt(b*b - 4*a*c))/(2*a);
   double x2 = ((-1)*b - sqrt(b*b - 4*a*c))/(2*a);

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote