Question

1. Typically, arrays are passed by reference in programming languages since they can be arbitrarily long and copying each element may take time and space. How are arrays passed to functions in C++? If by reference, is there a way to pass them by value? If so, how? Show coded examples and output.

2. Can separate blocks be specified in C++, and if so, how is variable scoping handled? Show coded examples and output. For example:

int main() // declare variables { // declare variables (maybe also with same name as above) // and so on... for (...) // make

3. Can the goto statement be utilized in C++? If so, what do you go to? Show coded examples and output.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer 1:
Yes when we wrap the array inside the structure than we can pass array as call by value

struct ArrayStruct
{
int arr[10];
};
  
void change(struct ArrayStruct temp)
{
int *ptr = temp.arr;
int i;
  
for (i = 0; i < 10; ++i)
ptr[i] = 100; // OR *(ptr + i)
  
}
  
int main()
{
int i;
struct ArrayStruct obj;
for (i=0; i<10; i++)
obj.arr[i] = 10;
  
for (i = 0; i < SIZE; ++i)
cout<<obj.arr[i]<<" ";
cout<<endl;
  
change(obj);
  
  
  
for (i = 0; i < SIZE; ++i)
cout<<obj.arr[i]<<" ";
  
return 0;
}

Answer 2:
Yah we can have different blocks in cpp
So the variables created inside the block will accessable inside the block and to its child blocks
int main(){
   int x=1;
   {
       int x=2;
       cout<<<x<<endl; // 2 will print
       {
           cout<<x<<endl; // 2 will print
       }
   }
   cout<<x<<endl; // 1 will print
}

Answer 3:
we can use goto in cpp to jump the control from one location to another location
int main(){
   int x=10;
   PRINT:
   cout<<x<<endl;
   x--;
   if x>10:
       goto PRINT;
      
}

Add a comment
Know the answer?
Add Answer to:
1. Typically, arrays are passed by reference in programming languages since they can be arbitrarily long...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Program Overview This brief exercise is designed for you to consider how reference variables behave when...

    Program Overview This brief exercise is designed for you to consider how reference variables behave when you are passing them as arguments by-value. Instructions Name your class References.java. 1. Implement the following methods. 1.1 Method name: readStudentNames Purpose: This method accepts an array of strings as a parameter. It loops through the array and asks the user to input names. It populates the array of strings with the names. Parameters: an array of Strings, stringArray Return type: void In the...

  • Programming Assignment 1 Structures, arrays of structures, functions, header files, multiple code files Program description: Read...

    Programming Assignment 1 Structures, arrays of structures, functions, header files, multiple code files Program description: Read and process a file containing customer purchase data for books. The books available for purchase will be read from a separate data file. Process the customer sales and produce a report of the sales and the remaining book inventory. You are to read a data file (customerList.txt, provided) containing customer book purchasing data. Create a structure to contain the information. The structure will contain...

  • can someone please comment through this code to explain me specifically how the variables and arrays...

    can someone please comment through this code to explain me specifically how the variables and arrays are working? I am just learning arrays code is below assignment C++ Programming from Problem Analysis to Program Design by D. S. Malik, 8th ed. Programming Exercise 12 on page 607 Lab9_data.txt Jason, Samantha, Ravi, Sheila, and Ankit are preparing for an upcoming marathon. Each day of the week, they run a certain number of miles and write them into a notebook. At the...

  • Lab #7 CSE110 - Arizona State University Topics • Basic arrays Coding Guidelines • Give identifiers...

    Lab #7 CSE110 - Arizona State University Topics • Basic arrays Coding Guidelines • Give identifiers semantic meaning and make them easy to read (examples numStudents, grossPay, etc). • Keep identifiers to a reasonably short length. • Use upper case for constants. Use title case (first letter is upper case) for classes. Use lower case with uppercase word separators for all other identifiers (variables, methods, objects). • Use tabs or spaces to indent code within blocks (code surrounded by braces)....

  • Hi, this is an intro to C Programming Class. Please do not use arrays, functions, math.h,...

    Hi, this is an intro to C Programming Class. Please do not use arrays, functions, math.h, or struct to store any data except to declare strings char strVar[20]. Furthermore, can you please do a good check to make sure the final program does not have any problems because when I copy and paste them the app I use says there are some problems and it won't run the program. So if you can double check to make sure that everything...

  • Please use comment headers to specify what each section of code does. The programming language is...

    Please use comment headers to specify what each section of code does. The programming language is C. Skeleton code: Sample output: Obiectives 1. To learn how to write functions given specifications 2. To learn how to the use pass by value and pass by reference variables 3. Review previous concepts like if-statements and loops. Movies about dragons and dragon training were very popular this summer. Your fiend has not stopped talking about how awesome dragons are and how cool it...

  • Introduction to C Programming – COP 3223 1. To learn how to use arrays to store...

    Introduction to C Programming – COP 3223 1. To learn how to use arrays to store and retrieve data to help solving problems. 2. Reinforce use of input files. Introduction: Ninja Academy Ninjas are awesome! Your friend has not stopped talking about how cool ninjas and how they would like to become a ninja. To amuse your friend, you have decided to create a series of programs about ninjas. Problem: Mentorship (ninjamentors.c) It is time for your friend to select...

  • C Programming write two functions, similar to what you see in the sample program. The first will ask the user to enter some information (I have included the type in parentheses) First Name (char[]) L...

    C Programming write two functions, similar to what you see in the sample program. The first will ask the user to enter some information (I have included the type in parentheses) First Name (char[]) Last Name (char[]) Age (int) Height in Inches (double) Weight in Pounds (double) You will use pass-by-reference to modify the values of the arguments passed in from the main(). Remember that arrays require no special notation, as they are passed by reference automatically, but the other...

  • Using C programming language Question 1 a) through m) Exercise #1: Write a C program that...

    Using C programming language Question 1 a) through m) Exercise #1: Write a C program that contains the following steps (make sure all variables are int). Read carefully each step as they are not only programming steps but also learning topics that explain how functions in C really work. a. Ask the user for a number between 10 and 99. Write an input validation loop to make sure it is within the prescribed range and ask again if not. b....

  • This C++ Programs should be written in Visual studio 2017 PROGRAM 1: Comparing Using Arrays to...

    This C++ Programs should be written in Visual studio 2017 PROGRAM 1: Comparing Using Arrays to Using Individual Variables One of the main advantages of using arrays instead of individual variables to store values is that the program code becomes much smaller. Write two versions of a program, one using arrays to hold the input values, and one using individual variables to hold the input values. The programs should ask the user to enter 10 integer values, and then it...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT