• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Little C++ homework

paweleq2000

New Member
Joined
Feb 18, 2011
Messages
161
Reaction score
4
Hello i need help with these exercises:

1. Write Program, Which allows the user to enter 5 integers. Program must load the sum of Those That are numbers greater than 1 and smaller than 10.

2. Write a program in which you create an array of 10 integer element. The program is the intended to fill all the places in the table, but only by odd numbers (1,3,5 ..)

3. Write a program in which you create a function that allows the calculation and determination of the sum of 3 integers given by the parameters of this function.

4. Write a function That for any one dimensional array show us smallest element of array

5. Write a program from the previous task to set up on dynamic array.
 
Ask @ StackOverflow for quick replies :)
 
you should at least try a bit instead of looking for answers, otherwise you won't learn o_O
 
I'm trying, and such questions may be on the exam, so I want also to teach them and follow carefully
 
what ones have you already started on?
post what you've come up with so far
 
number 2
Code:
#include <iostream>

using namespace std;

int main()
{
   int a;
   int tab[10];

  for(int i=0;i<10;i++)
  {

     do
     {
         cin>>a;
     }while(a%2==0);

     tab[i]=a;
  }
  for(int i=0;i<10;i++)
cout<<tab[i]<<", ";


   return 0;
}
 
number 2
Code:
#include <iostream>

using namespace std;

int main()
{
   int a;
   int tab[10];

  for(int i=0;i<10;i++)
  {

     do
     {
         cin>>a;
     }while(a%2==0);

     tab[i]=a;
  }
  for(int i=0;i<10;i++)
cout<<tab[i]<<", ";


   return 0;
}
Code:
#include <iostream>
#include <sstream>

int inputNumber(bool retry)
{
    // Send retry message if function was called with retry as true (should only happen if user enters an even number)
    if (retry) {
        std::cout << "You entered an even number, try again." << std::endl;
    }

    uint64_t num;
    std::cout << "Enter an odd number: ";
    std::cin >> num;
    if (num % 2 == 0) {
        // Execute function again looking for an odd number
        inputNumber(true);
    }
    // Return the odd number given
    return num;
}

int main()
{
    // Assign array values
    int tab[5];
    for (uint8_t i = 0; i < 5; i++) {
        tab[i] = inputNumber(false);
    }

    // Print back array values
    std::ostringstream stream;
    for (uint8_t i = 0; i < 5; i++) {
        stream << tab[i] << " ";
    }
   
    std::cout << stream.str();

    return 1;
}
 
yeah dont forget comments thats probably the most points you getting for it on the exam
 
Code:
#include <iostream>
#include <sstream>

int inputNumber(bool retry)
{
    // Send retry message if function was called with retry as true (should only happen if user enters an even number)
    if (retry) {
        std::cout << "You entered an even number, try again." << std::endl;
    }

    uint64_t num;
    std::cout << "Enter an odd number: ";
    std::cin >> num;
    if (num % 2 == 0) {
        // Execute function again looking for an odd number
        inputNumber(true);
    }
    // Return the odd number given
    return num;
}

int main()
{
    // Assign array values
    int tab[5];
    for (uint8_t i = 0; i < 5; i++) {
        tab[i] = inputNumber(false);
    }

    // Print back array values
    std::ostringstream stream;
    for (uint8_t i = 0; i < 5; i++) {
        stream << tab[i] << " ";
    }
  
    std::cout << stream.str();

    return 1;
}
By his description it didn't sound like it was meant to take user input. From his code it seems he misunderstood as well, or the description is vague.
 
By his description it didn't sound like it was meant to take user input. From his code it seems he misunderstood as well, or the description is vague.
he used std::cin from when he started the problem, so i did the same
his descriptions are vague though so it isn't exactly easy to tell what he wants
i also didn't need to use ostringstream cause i could have used cout without starting newlines
 
Sorry for the quality:
1:
Code:
#include <iostream>
#include <vector>

int main()
{
    std::vector<long long> integers;
    integers.reserve(5);

    std::string input;

    while (integers.size() < 5) {
        std::cout << std::endl << "Please type a number: ";
        std::cin >> input;

        integers.push_back(std::atoll(input.c_str()));
    }

    unsigned long sum = 0;
    for (const unsigned long integer : integers) {
        if (integer > 1 && integer < 10) {
            sum += integer;
        }
    }

    std::cout << std::endl << std::endl << "Sum: " << sum << std::endl;

    return 0;
}

2:
Code:
#include <iostream>

int main()
{
    unsigned integers[10];

    unsigned index = 0, i = 0;

    while (true) {
        if (++i % 2 != 0) {
            integers[index] = i;

            if (++index == 10) {
                break;
            }
        }
    }

    for (index = 0; index < 10; ++index) {
        std::cout << integers[index] << " ";
    }

    std::cout << std::endl;

    return 0;
}

3, which I don't understand:
Code:
//No idea what calculation and determination is.

#include <iostream>

int main()
{
    int a = 1, b = 3, c = 10;

    std::cout << [&]() -> int { return a + b + c; }() << std::endl;

    return 0;
}

4 & 5(?):
Code:
#include <iostream>

int findSmallestInt(int *array, size_t size)
{
    int smallest = array[0];

    for (size_t i = 0; i < size; ++i) {
        if (smallest > array[i]) {
            smallest = array[i];
        }
    }

    return smallest;
}

int main()
{
    int array[10] = {
        75, 10, 12, 40, 7, 9, 81, 5, 99, 24
    };

    std::cout << findSmallestInt(array, sizeof array / sizeof array[0]) << std::endl;

    return 0;
}
 
Back
Top