Question

In: Computer Science

#include <stdio.h> void printDistinct(int arr[], int c) { int i, j; printf("\nArray:\n"); // Picking all elements...

#include <stdio.h>

void printDistinct(int arr[], int c)
{
int i, j;
printf("\nArray:\n");
// Picking all elements one by one
for (i = 0; i < c; i++)
{
// Checking if the picked element is already printed
for (j = 0; j <= i; j++)
{
// If current element is already there in the array, break from j loop
if (arr[i] == arr[j])
{
break;
}
}
// If it is not printed earlier and is within 10-100, then we print it
if (i == j && arr[i] > 10 && arr[i] < 100)
printf("%d ", arr[i]);
}

}

int main()
{
// to test above function
int arr[20], i;
printf("Enter 20 numbers between 10-100\n");
for (i = 0; i < 20; i++)
{
scanf("%d", &arr[i]);
}
int c = sizeof(arr) / sizeof(arr[0]);
printDistinct(arr, c);

int a[20], j, k, t, n = 20;
  
//-------------------------------------------------------------------------------------
for (i = 0; i < n; i++) //Sort array in ascending order
{
for (j = i + 1; j < n; j++)
{
if (a[j] < a[i]) //Check jth element smaller than ith element
{
t = a[i]; //if yes,assign the ith value to a temporary variable
a[i] = a[j]; //Assign the value of jth element to i.
a[j] = t; //Then assign value of temporary variable to j.
}
}
}
printf("\nSorted array is: "); //Finally print sorted array
for (i = 0; i < n; i++)
{
printf("%d ", a[i]);
}
return 0;
}

and the output for sorted is this

-858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460

what i need to do to make sure that the numbers are sorted properly.

Solutions

Expert Solution

Please find the updated code and output below.

#include <stdio.h>

void printDistinct(int arr[], int c)
{
int i, j;
printf("\nArray:\n");
// Picking all elements one by one
for (i = 0; i < c; i++)
{
// Checking if the picked element is already printed
for (j = 0; j <= i; j++)
{
// If current element is already there in the array, break from j loop
if (arr[i] == arr[j])
{
break;
}
}
// If it is not printed earlier and is within 10-100, then we print it
if (i==j && arr[i] > 10 && arr[i] < 100){
printf("%d ", arr[i]);

}
}
}
int main()
{
// to test above function
int arr[20], i;
printf("Enter 20 numbers between 10-100\n");
for (i = 0; i < 20; i++)
{
scanf("%d", &arr[i]);
}
int c = sizeof(arr) / sizeof(arr[0]);
printDistinct(arr, c);

int j, k, t, n = 20;
  
//-------------------------------------------------------------------------------------
for (i = 0; i < n; i++) //Sort array in ascending order
{
for (j = i + 1; j < n; j++)
{
if (arr[i] > arr[j]) //Check jth element smaller than ith element
{
t = arr[i]; //if yes,assign the ith value to a temporary variable
arr[i] = arr[j]; //Assign the value of jth element to i.
arr[j] = t; //Then assign value of temporary variable to j.
}
}
}
printf("\nSorted array is: "); //Finally print sorted array
for (i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
return 0;
}

OUTPUT :


Related Solutions

Please paraphrase this c code #include <stdio.h> #include <stdlib.h> #include <string.h> void sortGrades(int arr[], int size,...
Please paraphrase this c code #include <stdio.h> #include <stdlib.h> #include <string.h> void sortGrades(int arr[], int size, int status, char names[][20]); void printer(int grades[], int size, char names[][20]); void sortNames(char arr[][20], int size, int status, int grades[]); void nameSearch(int grades[], int size, char names[][20]); void numSearch(int grades[], int size, char names[][20]); int main() { int i; int size; int option; do { printf("\n\nInput Number of Students or 0 to exit : "); scanf("%d", &size); if (size == 0) { break; }...
#include <stdio.h> int main() { int i, j; printf("Enter two positive integers: "); scanf("%d%d", &i, &j);...
#include <stdio.h> int main() { int i, j; printf("Enter two positive integers: "); scanf("%d%d", &i, &j); // need to validate both being positive while (i != j) { i > j ? (i -= j) : (j -= i); } printf("GCD: %d\n", i); return 0; } The above code returns a GCD from two numbers entered. Use the above program to design a C program to compute of integer coefficients a and b such that gcd(i, j) = a xi...
C programming #include <stdio.h> #include <math.h> int main() { printf("============== Problem #1 =================\n"); printf("This should print...
C programming #include <stdio.h> #include <math.h> int main() { printf("============== Problem #1 =================\n"); printf("This should print down from 2 to 0 by 0.1 increments\n"); float f = 2.0; while (f != 0) { printf("%0.1f\n",f); f = f - 0.1; } printf("============== Problem #2 =================\n"); printf("This should find that the average is 5.5\n"); int total_score = 55; int total_grades = 10; double avg = total_score/total_grades; printf("Average: %0.2f\n",avg); printf("============== Problem #3 =================\n"); printf("If the population increases by 2.5 people per second, how...
Consider the following code: void swap(int arr[], int i, int j) {        int temp = arr[i];...
Consider the following code: void swap(int arr[], int i, int j) {        int temp = arr[i];        arr[i] = arr[j];        arr[j] = temp; } void function(int arr[], int length) {        for (int i = 0; i<length / 2; i++)               swap(arr, i, (length / 2 + i) % length); } If the input to the function was int arr[] = { 6, 1, 8, 2, 5, 4, 3, 7 }; function(arr,8); What values would be stored in the array after calling the...
#include <stdlib.h> #include <stdio.h> #include <string.h> void clrScreen(int lines){     int i = 0;     for( i =...
#include <stdlib.h> #include <stdio.h> #include <string.h> void clrScreen(int lines){     int i = 0;     for( i = 0; i < lines; ++i ){         printf("\n");     }     return; } void printRules(void){     printf("\t|*~*~*~*~*~*~*~*~*~ How to Play ~*~*~*~*~*~*~*~*~*~|\n");     printf("\t|   This is a 2 player game. Player 1 enters the   |\n");     printf("\t|   word player 2 has to guess. Player 2 gets a    |\n");     printf("\t|   number of guesses equal to twice the number    |\n");     printf("\t|   of characters. EX: If the word is 'example'    |\n");     printf("\t|   player 2 gets 14 guesses.                      |\n");     printf("\t|*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~|\n");     clrScreen(10);     return; } //------------------------------------------------------------------------------------------------------------ /*...
#include <stdio.h> #include <math.h> int fun(int); int main(void)    {     int i = 5, x...
#include <stdio.h> #include <math.h> int fun(int); int main(void)    {     int i = 5, x = 3;     i = fun(x);     printf("%d\n", i);     return 0; } int fun(int i) {      int res = 0;      res = pow (i , 3.0);      return ( res); }
What’s the output of following code fragment: #include<stdio.h> #include<signal.h> void response (int sig_no) { printf("25"); }...
What’s the output of following code fragment: #include<stdio.h> #include<signal.h> void response (int sig_no) { printf("25"); } void response2 (int sig_no) { printf("43"); }     int main() {      int id = getpid();      signal(SIGUSR1, response); signal(SIGKILL, response2); for(int i=0; i<4; i++) { sleep(1); if (i % 3 == 0) { kill(id, SIGUSR1); } else { kill(id, SIGKILL); } } return 0; }
#include <stdio.h> #include <stdlib.h> // required for atoi int main(void) {     int i=0,n,num,filenum[100],pos;     int...
#include <stdio.h> #include <stdlib.h> // required for atoi int main(void) {     int i=0,n,num,filenum[100],pos;     int c;    char line[100]; //declaring string for storing data in the line of text    FILE *fp; // declaring a FILE pointer    fp=fopen("numbers.txt","r"); // open a text file for reading    while(fgets(line, sizeof line, fp)!=NULL) {       // looping until end of the file         filenum[i]=atoi(line); //converting data in the line to integer and storing it into array        i++;    }...
#include <stdio.h> int sum(int n); //prototype int main() {     int number, result;     printf("Enter a positive integer:...
#include <stdio.h> int sum(int n); //prototype int main() {     int number, result;     printf("Enter a positive integer: ");     scanf("%d", &number);     result = sum(number);     printf("sum = %d", result);     return 0; } int sum(int n) {     if (n != 0)         return n + sum(n-1);     else         return n; } What does the code above do?
#include <stdio.h> #include <stdlib.h> #include <time.h> void sort(int a[], int size); void printArray(int a[], int size);...
#include <stdio.h> #include <stdlib.h> #include <time.h> void sort(int a[], int size); void printArray(int a[], int size); int main(){ int arraySize, limit, count, srand(time(0)); print f("Enter the size of array\n"); scanf("%d", arraySize); int array[arraySize]; printf("Enter the upper limit\n"); scanf("%d", &limit); count = 0; while(count <= arraySize){ array[count] = (rand() % (limit + 1)); count++; } printArray(array, &arraySize); sort(array, arraySize); printArray(array, arraySize); Return 0; } void printArray(int a[], int size){ int i = 0; printf("Array: ["); while(i < size){ if(i != size...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT