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

Arrays Create a program that declares a variable that contains 10 int values and

ID: 3874652 • Letter: A

Question

Arrays
Create a program that declares a variable that contains 10 int values and displays the 10 values.

Change the previous program to display the values through calling a function that takes the array as a parameter.

Change the previous program to also call another function that doubles every second element of the array. Call this function after you display the original values. Then display the revised values before exiting the program.

Change the previous program to set the values in the array using getNum().

Change the previous program to repeat everything in the program 3 times (by using a loop).

Change the previous program to call getNum() before the loop starts and use that value to determine how often to do the loop.

Strings and Console Input
Write a program that declares a null-terminated string variable that can hold 20 characters (plus one character for the null-termination) and displays it using printf().

Change the previous program to use fgets() to get that string from the user.

Change the previous program to declare a second similar string variable, get the value for it using fgets(), and display that value.

Use the previous program but enter 30 characters for the first string. See what happens.

Change the previous program to not use printf(). Instead, display the string by using a function that you’ve created that loops, displaying one character at a time until you reach the null-termination (and then display a ‘ ’ at the end of the string).

Change the previous program to append the first string to the end of the second string after you get both values but before you display the second string.

Change the previous program to copy the first string to the second one instead of getting the second string from the user or appending the first string.

Change the previous program to use atoi() and atof() to convert the strings to numbers and display the numeric values.

Change the previous program to separate the different actions into functions that are called with the string variables passed as parameters.

Change the previous program to use sscanf() instead of atoi() and atof().

Change the previous program to make the desired input more complex (e.g. take in two ints and a word on the same line).

Change the program from practice problem #3 to compare the two strings and display something indicating the result.

For, Do-While, Switch, Break, Continue
Go back in the previous practice problems and replace loops with do-while where applicable.

Similar to the previous problem, replace loops with for statements where applicable.

In both of the previous practice problems, add breaks to get out of loops where applicable.

Create occasions to use continue statements in the previous practice problems (you might have to be creative).

Change the practice problem from the Strings and Console Input section that uses atoi() to use a switch statement that displays something when certain int values are entered.

Pointers
Write a program that declares a null-terminated string variable that can hold 20 characters (plus one character for the null-termination) and displays it using printf() (this is the same as a previous practice problem).

Change the previous program to declare a char * variable that points to the string variable. Use the pointer variable to display the string variable.

Change the previous program to use pointer arithmetic to display the contents of the variable one character at a time. Make sure that you stop at the null-termination.

Change the previous program to use an array of 10 ints instead of a string. You will have to loop to display the array but still use the pointer variable and pointer arithmetic.

Change the previous program to display the address of each element at the same time that you display the value of the element.

Write a program that implements the printAndClearInv() example found in the lecture slides.

Change the previous program to use pointers and pointer arithmetic in the printAndClearInv() function. You will still use an array in main() but you will not use an array in printAndClearInv().

Change the previous program to take two additional parameters to printAndClearInv(): the size of one element and the number of elements in the array. Change the data type of the current first parameter from being a pointer to int to being a pointer to char. Then use casting, along with the new parameters and a switch statement, to make printAndClearInv() work for elements of size 1, 2, 4, and 8 bytes. NOTE: This is harder than the previous ones.

Explanation / Answer

Hi i have written code in c# language. code is easy understandable.

1) Here I have written the program that it contains 10 integer value in the array and created one veriable i so it will increment 10 times and based on user choice value will enter in the array .

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace PracticeChegg

{

public class Exercise1  

{  

public static void Main()  

{  

int[] arr = new int[10];

int i;  

Console.Write("Input 10 elements in the array : ");  

for(i=0; i<10; i++)  

{  

Console.Write(" Element Entered - {0} : ",i);

arr[i] = Convert.ToInt32(Console.ReadLine());

}  

  

Console.Write(" Elements which entered in array are : ");  

for(i=0; i<10; i++)  

{  

Console.Write("{0} ", arr[i]);

Console.ReadLine();

}

Console.Write(" ");

}

}

2)Change the previous program to display the values through calling a function that takes the array as a parameter.

Ans-) I have changed the program and taken array as a parameter in the function and that function PrintArray calling in the main method.

public class Exercise1  
{  
public static void Main()  
{
int[] arry = new int[10];
PrintArray(arry);
for (int j = 0; j < arry.Length; j++)
{
System.Console.Write(arry[j] + "{0}", j < arry.Length - 1 ? " " : "");
}
Console.Write(" Elements in array are: ");
System.Console.WriteLine();
Console.ReadLine();   
}

static void PrintArray(int[] arr)
{   

int i;
for (i = 0; i < 10; i++)
{   
arr[i] = Convert.ToInt32(Console.ReadLine());
} }

}

3)Change the previous program to also call another function that doubles every second element of the array. Call this function after you display the original values. Then display the revised values before exiting the program.

Ans- Here i have create one more function Seconddouble() in this function every second value will be double of its previosly enterd value .I am calling this function in the main method.

namespace PracticeChegg
{
public class Exercise1  
{  
public static void Main()  
{
int[] arry = new int[10];
PrintArray(arry);
Seconddouble(arry);
Console.Write(" Elements in array are: ");
for (int j = 0; j < arry.Length; j++)
{
System.Console.Write(arry[j] + "{0}", j < arry.Length - 1 ? " " : "");
}

System.Console.WriteLine();
Console.ReadLine();   
}
static void PrintArray(int[] arr)
{
int i;
for (i = 0; i < 10; i++)
{
// Console.Write("element - {0} : ", i);
arr[i] = Convert.ToInt32(Console.ReadLine());
}
}   
static void Seconddouble(int[] arrays)
{
int i;
for (i = 0; i <10; i++)
{
if(i%2==0){
arrays[i] = arrays[i];

}
else{
arrays[i] =2*arrays[i];
}
}
}

}

5) Change the previous program to repeat everything in the program 3 times (by using a loop).

Ans -Here i have created variable k that iterate 3 time and printing same out 3 times

namespace PracticeChegg
{
public class Exercise1  
{  
public static void Main()  
{
int[] arry = new int[10];
PrintArray(arry);
Seconddouble(arry);
Console.Write(" Elements in array are: ");

for (int k = 0; k < 3; k++)
{
for (int j = 0; j < arry.Length; j++)
{
System.Console.Write(arry[j] + "{0}", j < arry.Length - 1 ? " " : "");
}

System.Console.WriteLine();
Console.ReadLine();
}
}
static void PrintArray(int[] arr)
{
int i;
for (i = 0; i < 10; i++)
{
// Console.Write("element - {0} : ", i);
arr[i] = Convert.ToInt32(Console.ReadLine());
}
}   
static void Seconddouble(int[] arrays)
{
int i;
for (i = 0; i <10; i++)
{
if(i%2==0){
arrays[i] = arrays[i];

}
else{
arrays[i] =2*arrays[i];
}
}
}

}

Thank you

I you find helpful please give thumb :)

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