Pages

Wednesday, October 23, 2013

Assignment 2 : Programming


1.  Reprogram the coding below using c++ programming language and print the output





1)

#include <iostream>
using namespace std;

int main()
{
   
    int val[3][4]={8,16,9,52,3,15,27,6,14,25,2,10};
   
    cout << val[0][0]<<val[0][1]<<val[0][2]<<val[0][3] <<endl;
    cout << val[1][0]<<val[1][1]<<val[1][2]<<val[1][3] <<endl;
    cout << val[1][0]<<val[1][1]<<val[1][2]<<val[1][3] <<endl;
    cout << val[2][0]<<val[2][1]<<val[2][2]<<val[2][3] <<endl;
   
    system("PAUSE");
   
    return 0;
}




Output


8  16 9  52
3  15 27 6
14 25 2  10  















2)

#include <iostream>
using namespace std;

int main()
{
   
    int i,j,val[3][4]={8,16,9,52,3,15,27,6,14,25,2,10};
   
    cout << "Display of val array by explicit element" <<endl;
   
    for(i=0;i<3;++i)
    {
        cout <<"\n" <<endl;
       
        for(j=0;j<4;++j)
        cout <<val[i][j];
        cout << endl;
    }

    system("PAUSE");
   
    return 0;
}



Output

8  16 9  52
3  15 27 6
14 25 2  10  

















2.  A common operation is to determine the minimum or the maximum value stored in an array. Write a program that declare and initialize an array named score as follows.


int score[ 10 ] = { 4, 1, 5, 3, 4, 10, 9, 2, 1, 7 };





#include<stdio.h>

int main ()
{
    int i;
    int score[10]={ 4, 1, 5, 3, 4, 10, 9, 2, 1, 7 };
    int max = score[0];
    int min = score[0];
   
    for (i = 0; i < 10; i++)
    {
        if (score[i] > max)
      
            max = score[i];
     
        else if (score[i] < min)
      
            min = score[i];
      
    }
    cout << "\n Maximum element in an array : "<< max;
    cout << "\n Minimum element in an array : "<< min ; 

    system("PAUSE"); 
    return 0;
}














3.  complete the coding given. Print the output for the program function below:



#include <iostream>
using namespace std;

void somefunction(int[], int);

int main()
{
    const int SIZE = 10;
    int a[SIZE] = { 8, 3, 1, 2, 6, 0, 9, 7, 4, 5};
   
    cout << "Answer is:\n";
    somefunction(a, SIZE);
    cout << endl;
    system("pause");
    return 0;
}

void somefunction(int b[], int size)
{
    if (size > 0)
    {
        somefunction(&b[1], size - 1);
        cout << b[0] << " ";
    }
}



Output

Answer is:
5 4 7 9 0 6 2 1 3 8












4.      Write a variable function prototype for the following statement:




1)   Function evenNumber that takes an integer argument,number and return an integer result.



int nevenNumber(int 1,int 2);




2)   Function biggest that takes three integers x, y, z and returns an integer.



int biggest(int x,int y,int z);




3)   Function largeNumber that takes two double-precision arguments.



void largeNumber(float a,float b);
















References



3.            http://www.tenouk.com/Module7.html

















No comments:

Post a Comment