I need some with this program, please look at the changes I need closely. Its running correctly, but I need to do a few adjustments here is the list of changes I need:
1. All of the integers on a single line, sorted in ascending order.
2. The median value of the sorted array on a single line
3. The average of the sorted array on a single line
Here is the program:
#include<stdio.h>
#include<stdlib.h>
/* This places the Adds Value to the sorted array */
void Add( int arr[], int Value, int Size){
int i;
for(i=0;i<Size;i++)
arr[i]+=Value;
}
/* This places the Remove Value to the sorted array */
void Remove( int arr[], int Value, int Size) {
int i;
for(i=0;i<Size;i++)
arr[i]-=Value;
}
/* Returns the size of the array in BYTES */
long Size( int arr[]) {
return sizeof(arr);
}
/* Returns non-zero if Value is contained in array, 0 if Value is
not contained in the array, the size parameter is the number of
elements currently in the array)*/
int Contains( int arr[], int Value, int Size ) {
int i;
for(i=0;i<Size;i++)
if(arr[i]==Value)
return 1;
return 0;
}
int main(){
FILE *fp;
fp=fopen("numbers.txt","r");
int t;
int a[100],lastIndex=0,i,j=0;
float sum=0;
while( fscanf(fp,"%d",&t)!=EOF )
{
/* printf("\nThe Element is t=%d\n",t); */
sum+=t;
if(lastIndex==0)
a[lastIndex]=t;
else if(a[lastIndex]<=t){
a[lastIndex]=t;
}
else{
for(i=lastIndex;i>0;i--){
if(a[i]>=t)
a[i+1]=a[i];
else {
break;
}
}
a[i+1]=t;
}
for(i=0;i<=lastIndex;i++)
printf("%d\t",a[i]);
lastIndex++;
}
printf("\nThe Sorted array is :");
for(i=0;i<lastIndex;i++)
printf("%d\t",a[i]);
float median;
if(lastIndex%2!=0)
median=a[lastIndex/2];
else
median=(a[lastIndex/2]+a[lastIndex/2-1])/2.0;
printf("\nThe median of the sorted array: %.2f",median);
printf("\nThe average of the sorted array:
%.2f",sum/lastIndex);
return 0;
}
I have correcetd your code You were having mistake in sorting part I am listing down your mistakes here and also commented in code where i have change the code .
1.In else part you were checking a[lastIndex]<=t but it should be :
else if (a[lastIndex - 1] <= t) //here we will check at lastIndex -1 for less then condition with t
{
a[lastIndex] = t;
}
2.In for loop part ,loop will range from LastIndex-1 to 0:
else
{
for (i = lastIndex - 1; i >= 0; i--) //here Range should be from index-1 to i>=0
{
if (a[i] >= t)
a[i + 1] = a[i];
else
{
break;
}
}
a[i + 1] = t;
}
Here I writing the corrected code :
#include <stdio.h>
#include <stdlib.h>
/* This places the Adds Value to the sorted array */
void Add(int arr[], int Value, int Size)
{
int i;
for (i = 0; i < Size; i++)
arr[i] += Value;
}
/* This places the Remove Value to the sorted array */
void Remove(int arr[], int Value, int Size)
{
int i;
for (i = 0; i < Size; i++)
arr[i] -= Value;
}
/* Returns the size of the array in BYTES */
long Size(int arr[])
{
return sizeof(arr);
}
/* Returns non-zero if Value is contained in array, 0 if Value is not contained in the array, the size parameter is the number of elements currently in the array)*/
int Contains(int arr[], int Value, int Size)
{
int i;
for (i = 0; i < Size; i++)
if (arr[i] == Value)
return 1;
return 0;
}
int main()
{
FILE *fp;
fp = fopen("numbers.txt", "r");
int t;
int a[100], lastIndex = 0, i, j = 0;
float sum = 0;
while (fscanf(fp, "%d", &t) != EOF)
{
/* printf("\nThe Element is t=%d\n",t); */
sum += t;
if (lastIndex == 0)
a[lastIndex] = t;
else if (a[lastIndex - 1] <= t) //here we will check at lastIndex -1 for less then condition with t
{
a[lastIndex] = t;
}
else
{
for (i = lastIndex - 1; i >= 0; i--) //here Range should be from index-1 to i>=0
{
if (a[i] >= t)
a[i + 1] = a[i];
else
{
break;
}
}
a[i + 1] = t;
}
// for (i = 0; i <= lastIndex; i++)
// printf("%d\t", a[i]);
lastIndex++;
}
printf("\nThe Sorted array is :");
for (i = 0; i < lastIndex; i++)
printf("%d ", a[i]);
float median;
if (lastIndex % 2 != 0)
median = a[lastIndex / 2];
else
median = (a[lastIndex / 2] + a[lastIndex / 2 - 1]) / 2.0;
printf("\nThe median of the sorted array: %.2f", median);
printf("\nThe average of the sorted array: %.2f", sum / lastIndex);
return 0;
}
Output:
In case You have any doubt feel free to ask .If you are Satisfied with answer please upvote Thanks.
I need some with this program, please look at the changes I need closely. Its running...
need help editing or rewriting java code, I have this program running that creates random numbers and finds min, max, median ect. from a group of numbers,array. I need to use a data class and a constructor to run the code instead of how I have it written right now. this is an example of what i'm being asked for. This is my code: import java.util.Random; import java.util.Scanner; public class RandomArray { // method to find the minimum number in...
How can I convert the following C code to MIPS Assembly? +++++++++++++++++++++++++++++++++ MIPS main program ++++++++++++++++++++++++++++++++ .data # Defines variable section of an assembly routine. array: .word x, x, x, x, x, x, x, x, x, x # Define a variable named array as a word (integer) array # with 10 unsorted integer numbers of your own. # After your program has run, the integers in this array # should be sorted. .text # Defines the start of the code...
I need the pseudocode for this c program source code. #include<stdio.h> void printarray(int array[], int asize){ int i; for(i = 0; i < asize;i++) printf("%d", array[i]); printf("\n"); } int sum(int array[], int asize){ int result = 0; int i = 0; for(i=0;i < asize;i++){ result = result + array[i]; } return result; } int swap( int* pA,int*pB){ int result = 0; if(*pA > pB){ int tamp = *pA; *pA = *pB; *pB = tamp; result = 1; } return result;...
Please I need help in C language, I am trying to modify the code per the below instructions, but I am getting errors. Can't fgure it out. The attempted code modification and data file is privided below, after the instructions. Thank you. Instructions: 1. First, add a function named printMsg that displays a message, a greeting, or an introduction to the program for the user. Add a statement that calls that function from the main function when your program starts....
c++ please read all question edit the program to test different random sizes of the array and give me the time in a file will be like random size of the array and next to it the time it took for each size Im trying to do time analysis for Quick sort but i keep getting time = 0 also i want edit the program to test different random sizes of the array and give me the time in a...
C programming (you don't need to write program) Problem 1 [Linear Search with Early Stop] Below you will find a linear search function with early stop. A linear search is just a naive search - you go through each of the elements of a list one by one. Early stop works only on sorted list. Early stop means, instead of going through whole list, we will stop when your number to search can no longer be possibly found in the...
I need to write a program in java that reads a text file with a list of numbers and sorts them from least to greatest. This is the starter file. import java.util.*; import java.io.*; public class Lab3 { static final int INITIAL_CAPACITY = 5; public static void main( String args[] ) throws Exception { // ALWAYS TEST FOR REQUIRED INPUT FILE NAME ON THE COMMAND LINE if (args.length < 1 ) { System.out.println("\nusage: C:\\> java Lab3 L3input.txt\n"); System.exit(0); } //...
The goal is to generate some random number and output a sorted list by quicksort. what do I need to add in my main to accomplish that? i also want it to output the run time. thank you #include "pch.h" #include <iostream> using namespace std; /* C implementation QuickSort */ #include<stdio.h> void swap(int* a, int* b) { int t = *a; *a = *b; *b = t; } int partition(int arr[], int low, int high) { ...
#include <sys/types.h> #include <sys/ipc.h> #include <sys/shm.h> #include <sys/wait.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include<time.h> void insertionSort(int arr[], int n); void merge(int a[], int l1, int h1, int h2); void mergeSort(int a[], int l, int h) { int i, len=(h-l+1); //Using insertion sort for small sized array if (len<=5) { insertionSort(a+l, len); return; } pid_t lpid,rpid; lpid = fork(); if(lpid<0) { //Lchild proc not created perror("Left Child Proc. not created\n"); _exit(-1); } else if (lpid==0) { mergeSort(a,l,l+len/2-1); _exit(0); } else...
C program-- the output is not right please help me to correct it. #include <stdio.h> int main() { int arr[100]; int i,j,n,p,value,temp; printf("Enter the number of elements in the array: \n "); scanf("%d",&n); printf("Enter %d elements in the array: \n",n); for(i=0;i<n;i++) { printf("\nelement %d: ",i); scanf("\n%d",&arr[i]); } printf("\nEnter the value to be inserted: \n "); scanf("\n%d",&value); printf("The exist array is: \n"); for(i=0;i<n;i++) { printf("%d",arr[i]); } p=i; for(i=0;i<n;i++) if(value<arr[i] ) { p = i; break; } arr[p]=value; printf("\n"); for (i =...