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

1. Write a program that creates an array containing all even numbers from 1 to 1

ID: 3665925 • Letter: 1

Question

1. Write a program that creates an array containing all even numbers from 1 to 100 in ascending order. Display the 5 largest even numbers from the array in descending order. The output should look as follows:

Top 5 even numbers are: 100 98 96 94 92

2. Write a program that implements a two dimensional array. Prompt the user to input 5 integer values and then calculate and store the squared values for each user input in the array. Output the results to the user.

Value

Squared

5

25

8

64

2

4

10

100

1

1

3. Write a program that uses a 10-element integer array and prompts the user for 10 integer values to populate the array. Pass a pointer of this array to a function called sort() that sorts the numbers from smallest to largest and outputs the sorted numbers to the user.

4. Write a program that uses a 10-element integer array and prompts the user for 10 integer values to populate the array. Pass a pointer of this array to a function called largest() that you create as part of your program. The function largest() then returns the largest value stored in the array. The function main() outputs the largest value contained in the array.

5. Write a program that prompts the user for a character string. Pass this string to a function called reverse() that you create as part of your program. The function reverse() then reverses the order of all of the characters in the string. The function main() then prints out the reversed string array.

Value

Squared

5

25

8

64

2

4

10

100

1

1

Explanation / Answer

1.

/** The C - program that creates an array of even numbers
of array size of 50. Then print top 5 even numbers from last.
*/
//header files
#include<stdio.h>
#include<conio.h>
int main()
{

   //set size
   const int size=50;
   //create an array of size
   int even[size];

   //set start =2
   int start=2;

   //run for loop to assign first 50 even numbers
   for(int index=0;index<50;index++)
   {      
           even[index]=start;
           //increment by two
           start=start+2;
   }


   printf("Top 5 even numbers are :");
   //print top 5 even numbers from last
   for(int index=1;index<=5;index++)
       printf("%3d",even[size-index]);


   //pause program output
   getch();
   return 0;
}
Sample output:
Top 5 even numbers are :100 98 96 94 92
-------------------------------------------------------------------------
2)

/**The C program that prompts to ener five values
and finds its square values and print to console*/
//header files
#include<stdio.h>
#include<conio.h>
int main()
{

   //set size
   const int size=5;
   //create an array of size=5
   int arr[size];
   //read 5 values in array
   for(int index=0;index<5;index++)
   {
       printf("Enter value %d ",(index+1));
       scanf("%d",&arr[index]);
   }

   //print value and its square
   printf("%-10s%-10s ","Value", "Square");
   for(int index=0;index<5;index++)
   {
       //print value and it's square
       printf("%-10d%-10d ",arr[index],arr[index]*arr

[index]);
   }


   getch();
   return 0;
}


Sample output:
Enter value 1 5
Enter value 2 8
Enter value 3 2
Enter value 4 10
Enter value 5 1
Value     Square
5         25
8         64
2         4
10        100
1         1
------------------------------------------------------------------------------------------

3)


/**The program prompts 10 elements and sorts the numbers
from smallest to largest by passing pointer to the fuction*/
//header files
#include<stdio.h>
#include<conio.h>
void sort(int*ptr, int length);
int main()
{

   //create an array of size=10
   int arr[10];
   //create a pointer array
   int *ptr;
  
   //read 10 values in array
   for(int index=0;index<10;index++)
   {
       printf("Enter value %d ",(index+1));
       scanf("%d",&arr[index]);
   }
   //set address of arr to pointer
   ptr=arr;

   //call sort method with pointer as argument
   sort(ptr,10);
   printf("Sorted numbers : ");
   //print sorted elemets
   for(int index=0;index<10;index++)
   {
       printf("%3d",ptr[index]);
   }


   getch();
   return 0;
}

//Sort elements from smallest to largest
void sort(int*ptr,int length)
{

   for(int i=0;i<length;i++)
   {
       for(int j=0;j<length-1;j++)
       {
           if(ptr[j]>ptr[j+1])
           {

               int temp=ptr[j];
               ptr[j]=ptr[j+1];
               ptr[j+1]=temp;
           }
       }      
   }
}

sample output:

Enter value 1 10
Enter value 2 9
Enter value 3 8
Enter value 4 7
Enter value 5 6
Enter value 6 5
Enter value 7 4
Enter value 8 3
Enter value 9 2
Enter value 10 1
Sorted numbers :   1 2 3 4 5 6 7 8 9 10

------------------------------------------------------------------------------------------

------

4

/**The program prompts 10 elements and calls largest
method to find the largest number and print the largest number in main*/
//header files
#include<stdio.h>
#include<conio.h>
int largest(int*ptr, int length);
int main()
{

   //create an array of size=10
   int arr[10];
   //create a pointer array
   int *ptr;
  
   //read 10 values in array
   for(int index=0;index<10;index++)
   {
       printf("Enter value %d ",(index+1));
       scanf("%d",&arr[index]);
   }
   //set address of arr to pointer
   ptr=arr;

   //call largest method with pointer as argument  
   printf("Largest number : %d",largest(ptr,10));

   getch();
   return 0;
}

//Returns the largest number
int largest(int*ptr,int length)
{
   int largest=ptr[0];

   for(int i=1;i<length;i++)
   {
       if(ptr[i]>largest)
           largest=ptr[i];
   }

   return largest;
}

sample output:
Enter value 1 10
Enter value 2 9
Enter value 3 8
Enter value 4 7
Enter value 5 6
Enter value 6 5
Enter value 7 4
Enter value 8 3
Enter value 9 2
Enter value 10 1
Largest numbr :   10

----------------------------------------------------------------------------------------
5.

/**The c program that promts the string and calls
reverse function and prints the string in reverse order*/
//header files
#include<stdio.h>
#include<string>
#include<conio.h>
void reverse(char sring[]);
int main()
{

   const int size=50;
   char str[size];

   printf("Enter string ");
   scanf("%s",str);

   //Call reverse
   reverse(str);

   printf("%s",str);
   //pause program output on console
   getch();
   return 0;
}

//The method reverse that takes c-string as
//argument and reverse the string
void reverse(char str[])
{
   int i = 0;
   //get lenght
   int j = strlen(str) - 1;

   //reverse the string by swapping characters
   //first last and so on
   while (i < j)
   {
       int temp = str[i];
       str[i] = str[j];
       str[j] = temp;
       i++;
       j--;
   }
}

sample output:
Enter string:hello
olleh

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote