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

Hi, I am a bignner so please be pationt when you answer. Can you please help me

ID: 3672866 • Letter: H

Question

Hi, I am a bignner so please be pationt when you answer.

Can you please help me understand this code better.

#include <stdio.h>

int main()

{

int array[100], n, c, d, swap; // why array[100] and what does it refer to? Why swap is there? can i change n and c to different names or I am suppose to use only them.

  

printf("Enter number of elements ");

scanf("%d", &n);

  

printf("Enter %d integers ", n);

  

for (c = 0; c < n; c++) // what is this for?

scanf("%d", &array[c]); // what is this for?

  

for (c = 0 ; c < ( n - 1 ); c++) // what is this for?

{

for (d = 0 ; d < n - c - 1; d++) // what is this for?

{

if (array[d] > array[d+1]) // For decreasing order use <

{

swap = array[d]; // what is this for?

array[d] = array[d+1]; // what is this for?

array[d+1] = swap; // what is this for?

}

}

}

  

printf("Sorted list in ascending order: ");

  

for ( c = 0 ; c < n ; c++ ) // what is this for?

printf("%d ", array[c]); // what is this for?

return 0;

}

The second one

#include <stdio.h>

int main()

{

int array[100], search, c, n; //explaine this please

  

printf("Enter the number of elements in array ");

scanf("%d",&n);

  

printf("Enter %d integer(s) ", n);

  

for (c = 0; c < n; c++) // what is this for?

scanf("%d", &array[c]); // what is this for?

  

printf("Enter the number to search ");

scanf("%d", &search);

  

for (c = 0; c < n; c++) // what is this for?

{

if (array[c] == search) // what is this for?

{

printf("%d is present at location %d. ", search, c+1); // what is search and c+1 doing?

break;

}

}

if (c == n) // what is this for?

printf("%d is not present in array. ", search);

  

return 0;

}

Explanation / Answer

Hi, I have commented each line of code very nicely, just go through rach comment you will,get it.

#include <stdio.h>
int main()
{
   // your code is assuming that, user will enter maximum of 100 elements, thats why 100, you can change it to any umber
   // Yes , you can change the name of any varible, but make sure thast you need to change every where varible has used
  
int array[100], n, c, d, swap; // why array[100] and what does it refer to? Why swap is there? can i change n and c to different names or I am suppose to use only them.
  
// asking user to enter size of array, maximum 100
printf("Enter number of elements ");
scanf("%d", &n);
  
printf("Enter %d integers ", n);
  
// this loop asks uuser to enter n number of integers
for (c = 0; c < n; c++) // what is this for?
scanf("%d", &array[c]); // what is this for? -- scaning user input in array[0], array[1], arrray[3]......to array[n-1]
      
   // this code is for sorting array in decresing order, this is called bubble sort                                                       // array index starts from 0, that why 0 to n-1
// this loop iterates array from 0 to n-2
for (c = 0 ; c < ( n - 1 ); c++) // what is this for?
{   // now this loop will compare each consucutive pair starting with 0 to n-1-c
for (d = 0 ; d < n - c - 1; d++) // what is this for?
{
           // while comapring consucutive pair, if later element in array is greater than previous , then swap, so that minimum element
           // will resides towards end of array
if (array[d] > array[d+1]) // For decreasing order use <
{
               // once we find a lement a pair , where first lement is greater than second, swap it
               // so forst dtore value od rray[d] to a temp variblae 'swap'
swap = array[d]; // what is this for?
// now assign value of array[d+1] to aray[d]
array[d] = array[d+1]; // what is this for?
// assign prevuois value of array[d], that is stored in 'swap' to array[d+1]
array[d+1] = swap; // what is this for?
}
}
}
  
printf("Sorted list in ascending order: ");
  
// iterating through array to print soreted sequence
for ( c = 0 ; c < n ; c++ ) // what is this for?
printf("%d ", array[c]); // what is this for? -- this statement will print value of array[c]
return 0;
}

The second one

#include <stdio.h>

int main()
{
   // your code is assuming that, user will enter maximum of 100 elements, thats why 100, you can change it to any umber
   // Yes , you can change the name of any varible, but make sure thast you need to change every where varible has used
int array[100], search, c, n; //explaine this please
  
printf("Enter the number of elements in array ");
scanf("%d",&n);
  
printf("Enter %d integer(s) ", n);
  
// asking user to enter size of array, maximum 100
for (c = 0; c < n; c++) // what is this for?
scanf("%d", &array[c]); // what is this for? -- scaning user input in array[0], array[1], arrray[3]......to array[n-1]
  
printf("Enter the number to search ");
scanf("%d", &search); // asking a number , to search in array
  
for (c = 0; c < n; c++) // what is this for? // iterating through each element of array
{
if (array[c] == search) // what is this for? === cjecking that current elelment of array is
//                           equal to search element that has asked from user
{
printf("%d is present at location %d. ", search, c+1); // what is search and c+1 doing?
                                                       // since array index start from 0, but in real we starts our counting from 1, that why we add 1 to
                                                       // aaray index to match with real counting
break;
}
}
if (c == n) // what is this for? -- if we reached at end of array , so we sure that searched element is not present in arrray
printf("%d is not present in array. ", search);
  
return 0;
}

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