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

Can someone write code for me ? Many thanks! Mini-sort with functions. Create a

ID: 3671486 • Letter: C

Question

Can someone write code for me ? Many thanks!

Mini-sort with functions. Create a project titled Lab6_Swap. Revisit this program from one of the earlier lab assignments that sorts three numbers in the increasing order. Modify it so that it invokes a function to swap the values of two variables. You should use the swapping function from this program. The pseudocde for the main() function is as follows:

Lottery. In this assignment you are to code a program that selects 10 random non-repeating positive numbers (the lottery numbers), takes a single input from the user and checks the input with the ten lottery numbers. The user wins if her input matches one of the lottery numbers.

Study displaying arrays in functions described here. As you program your project, demonstrate to the lab instructor displaying an array passed to a function. Create a project titled Lab6_Lottery. Declare an array wins of 10 integer elements.

Define the following functions:

Define function initialize() that takes array wins[] as a parameter and assigns -1 to each element of the array.

Hint: Array elements are initialized to -1, which is outside of lottery numbers' range, to distinguish elements that have not been given lottery numbers yet.

Passing an array as a parameter and its initialization is done similar to the code in this program.

Define a function check() that takes a number and the array wins[] as parameters and returns true if the number matches one of the elements in the array, or false if none of the elements do. That is, in the function, you should write the code that iterates over the array looking for the number passed as the parameter. You may assume that the number that check() is looking for is always positive.

Hint: Looking for the match is similar to looking for the minimum number in this program.

Define a function draw() that takes array wins as a parameter and fills it with 10 random integers whose values are from 0 to 99. The numbers should not repeat.

Hint: Use srand(), rand() and time() functions that we studied earlier to generate appropriate random numbers from 0 to 99 and fill the array. Before the selected number is entered into the array wins, call function check() to make sure that the new number is not already in the array. If it is already there, select another number.

The pseudocode for your draw() function may be as follows:

Define function entry() that asks the user to enter a single number from 0 to 99 and returns this value.

Define function printOut() that outputs the selected numbers and user input.
Hint: Outputting the array can be done similar to echoing it in this program.

The pseudocode your function main() should be as follows:

Note: A program that processes each element of the array separately (i.e. accesses all 10 elements of the array for assig

Explanation / Answer

// Lab6_Swap.cpp

#include<iostream>
using namespace std;

void swap(int &x, int &y);

int main()
{
   int a, b, c;
   cout << "input three numbers" << endl;
   cin >> a >> b >> c;
   if (a > b)
   {
       swap(a, b);
   }
   if (a > c)
   {
       swap(a, c);
   }
   if (b > c)
   {
       swap(b, c);
   }
   cout << "Numbers in sorted order" << endl;
   cout << a << " " << b << " " << c << endl;
   return 0;
}

void swap(int &x, int &y)
{
   int temp = 0;
   temp = x;
   x = y;
   y = temp;
}

Lab6_Lottery.cpp

#include <iostream>
#include "time.h"
using namespace std;

void initialize(int wins[]);
bool check(int num, int wins[]);
void draw(int wins[]);
int entry();
void printOut(int wins[], int input);

int main()
{
   int wins[10];
   int input = 0;
   initialize(wins);
   draw(wins);
   input = entry();
   if (check(input,wins))
   {
       cout << "You won the lottery" << endl;
   }
   else
   {
       cout << "You did not win" << endl;
   }
   printOut(wins, input);
   return 0;
}

void initialize(int wins[])
{
   for (int i = 0; i < 10; ++i)
   {
       wins[i] = -1;
   }
}

bool check(int num, int wins[])
{
   for (int i = 0; i < 10; ++i)
   {
       if (wins[i] == num)
           return true;
   }
   return false;
}

void draw(int wins[])
{
   int count = 0;
   srand(time(NULL));
   while (count < 10)
   {
       int num = rand() % 100;
       if (!check(num,wins))
       {
           wins[count] = num;
           count++;
       }
   }
}

int entry()
{
   int input;
   cout << "Enter a number" << endl;
   cin >> input;
   return input;
}

void printOut(int wins[], int input)
{
   cout << "winning numbers in lottery are" << endl;
   for (int i = 0; i < 10; ++i)
   {
       cout << wins[i] << " ";
   }
   cout << endl;

   cout << "Number input by user is " << input << endl;
}

Sample output

Enter a number
33
You did not win
winning numbers in lottery are
38 85 35 62 67 96 44 14 90 55
Number input by user is 33

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