Sunday, 10 December 2017

CS201 - Introduction to Programming -- Assignment No.2 - Fall 2017 - Solution

Leave a Comment
Objectives:
To enable students to write, compile and execute a program in DevC++. Moreover to familiarize students with  the concepts of:

  • If else structure
  • Loops
  • Passing array to a function
  • Multidimensional array declaration
  • Multidimensional array manipulation

Assignment:
Write a program that will generate random numbers and that numbers will be stored in a two-dimensional array. Then display the generated values on the screen and then also print the even numbers from the generated array elements.

Task 1:

First of all, you are required to generate random numbers from 1 to 500. For this, you are required to create a function named as “generateArray. The generateArray will load two- dimensional array with random numbers from 1to 500. The size of the two-dimensional array will be 7*7.

Task 2:

Defined a function named as “showArray(), which will display the elements of the two- dimensional array on the screen. See the sample output.

Task 3:
            The final task is to find the even numbers in this array and display it on the console. For this purpose create a function findEvenNumber().

CS201 - Introduction to Programming -- Assignment No.2 - Fall 2017 - Solution


Solution:

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

// functions definations
void generateArray(int inArr[7][7])
{
       for (int i = 0; i<7; i++)//rows
       {
              for (int j = 0; j<7; j++)//columns
              {
                     inArr[i][j] = rand() % 500 + 1; //Random Values Generation
              }
       }
}

void showArray(int inArr[7][7])
{
       for (int i = 0; i<7; i++)//rows
       {
              for (int j = 0; j<7; j++)//columns
              {
                     cout << inArr[i][j] << "\t"; // Output on Console
              }
              cout << "\n";
       }
}

void evenArray(int inArr[7][7])
{
       for (int i = 0; i<7; i++)
       {
              for (int j = 0; j<7; j++)
              {
                     if (inArr[i][j] % 2 == 0)
                     {
                           cout << inArr[i][j] << "\t"; // Output Even Values on console
                     }
              }
       }
}
// -- main body
main()
{
       int randarr[7][7];
       srand(time(NULL));// Null Time Randow Initialization
                                    // Array Generation
       generateArray(randarr);
       // Raw Array Output
       cout << "Array Element......\n\n";
       showArray(randarr);
       // Even Array output
       cout << "\n\nEven Array......\n\n";
       evenArray(randarr);
       // pause console screen
       cout << "\n\n";
       system("pause");
}


If You Enjoyed This, Take 5 Seconds To Share It

0 Questions:

Popular Posts