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

1. Create and invoke a function that returns a value. Show an example of how thi

ID: 3824922 • Letter: 1

Question

1. Create and invoke a function that returns a value. Show an example of how this is done.

2. Create and invoke a function that does not return a value. This called a void function. Show an example of a void function and how it is called from the main program.

3. What is passing by value vs by reference. Show examples.

4. What is a function prototype? Show an example of a function prototype

5. Define what we mean by a variable’s scope and lifetime. Show an example to help you define this concept.

6. What is a one dimensional array. Show an example of declaring a one dimensional array.

7. Give an example of how you get data into a one dimensional array from the keyboard.

8. Give an example of how to display data from #7 above.

9. Give an example of how to pass a one-dimensional array to a function

10. Define parallel one-dimensional arrays and show an example that demonstrates the use of parallel arrays.

11. What is a twodimensional array. Show an example of declaring a two dimensional array.

12. Give an example of how you get data into a two dimensional array from the keyboard.

13. Give an example of how to display data from #12 above.

Explanation / Answer

Create and invoke a function that returns a value.Show an example of how this is done.


// Creating a function

double
func1(double x)

{

return x * x;
}


// Calling or invoking the function

double x = 5.5;

double val = func1(x);

__________________


2.Create
and invoke a function that does not return a value.This called a void function.Show an example
of a void function
and how it is called from the main program.


// Creating a function

void
credit(double amount)

{

balance = balance + amount;
}


// calling or invoking the function

double amount = 4500;

credit(amount);


______________________

3.What is passing by value vs by reference.Show examples.


Passing by value :

Calling the function by passing the value.Even if any operations done on that variable then that
changes will not reflect in actual variable.

Passing by reference :

Calling the function by passing the variable reference.If any operations done on that variable
then that changes will reflect in actual variable.


// pass by value

double
add(int val);

// pass by reference

double add(int& val);

________________

4)What is a function prototype? Show an example of a function prototype

Ans)

Function prototype: In function prototype we just having function name and parameters and return type.We don’t have function body which contains logic.

_________________

5)Define what we mean by a variable’s scope and lifetime. Show an example to help you define this concept.

Scope is nothing but to what point of code we can access a particular variable.

There are scopes like private,public,protected

Private:

Private members of a class are not available to another class of the same package (or) another package

Private members are available to the class itself.

So,the scope of the Private access specifiers is class itself.

Public:

Public members of the class are available anywhere in other classes of the same package (or) another package.

so,The scope of public access specifier is global scope.

Protected:

Protected members are available in other class of the same package.They cannot be accessable to other classes which are presented in another package.

But protected members are available to the subclasses even though those classes are available in other packages.

Default:

default memebers are available in another classes of the same package .They are not available in other packages.

So the scope of the default specifier is package scope.

public class Demo


private String personName=”Hello”;

public
int year = 1998;

protected
int value = 19;

default String
make =”Audi”;


Here we cannot access variable ‘personName’ anywhere outside the class.We can access private variables only in the class In which they have been declared.

Here we can access the variable ‘year’ anywhere outside the class or in anyother class which ae present in another packages.

Here we cannot access the variable ‘value’ by the class which is present in another package.Only sub class of Demo class can access the variable ‘value’ in another packages also.

_______________

6)What is a one dimensional array. Show an example of declaring a one dimensional array.

By using one dimensional array we can store the values which are limiting to one thing

EX: if we want to store the grades of a student in different tests we can use one dimensional array.

//Declaring a one dimensional array and assigning values to the array

int grades[]={89,98,87,76,56};

_____________________

7)Give an example of how you get data into a one dimensional array from the keyboard.

//creating an integer array of size 5

Int gades[]=new int[5];


// Scanner object is used to get the inputs entered by the user

Scanner sc = new Scanner(System.in);


// Getting the values entered by the user and populating those values into an array

for (int i = 0; i < grades.length; i++)

{

grades[i] = sc.nextInt();
}


_________________

8)Give an example of how to display data from #7 above.

//Displaying the elements in the grades[] array

for(int i=0;i<grades.length;i++)

{

System.out.println(grades[i]);
}


_________________

9)Give an example of how to pass a one-dimensional array to a function

//function prototype

void func1(int grades[]);


// calling the function by passing the grades array as argument

func1(grades);

______________

10)Define parallel one-dimensional arrays and show an example that demonstrates the use of parallel arrays.

int size=6;

String names[] = new String[size];

int grades[] = new int[size];


In an array we can store values which area of same type.We cannot store different types of values in an array.So we have to create parallel arrays to hold the information.

_________________

11)What is a twodimensional array. Show an example of declaring a two dimensional array.

If we want to store multiple students grades scores in different tests.We have to use two dimensional array.

int grades[][]=new int[5][4];

Here we declared two dimensional array which stores grades of 5 students in 4 tests.

___________________

12)Give an example of how you get data into a two dimensional array from the keyboard.

//Scanner object is used to get the inputs entered by the user

Scanner sc=new Scanner(System.in);


// Getting the values entered by the user and populating those values into an array

for (int i = 0; i < grades.length; i++)

{

for (int j = 0; j < grades[0].length; j++)

{

grades[i][j] = sc.nextInt();
}
}


13. Give an example of how to display data from #12 above.

// Displaying the elements in the two dimensional array

for (int i = 0; i < grades.length; i++)

{

for (int j = 0; j < grades[0].length; j++)

{

System.out.println(grades[i][j]);
}
}

__________________Thank You