I made a simple program that would just show the input as the output.
My main problem is that I want to sort the output from high to low.
Instead of being sorted from high to low, the output is just the same order as the input.
Can someone check my codes and see why it is not sorting.
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#define size 7
#include<stdlib.h>
struct books
{
int profit;
};
void load(struct books b[], int n)
{
int i;
for (i = 0; i < n; i++)
{
printf("Enter profit:\n");
scanf("%d", &b[i].profit );
}
}
void print(struct books b[], int n)
{
int i;
for (i = 0; i<n; i++)
{
printf("Profit is:%d\n",b[i].profit);
}
}
void sort(struct books b[], int n)
{
int i; int j;
books t;
for (i = 0; i < n-1; i++)
for (j = 0; j < n-1 ; j++)
if (b[j].profit < b[j + 1].profit)
{
t = b[j];
b[j] = b[j + 1];
b[j+1] = t;
}
}
void main()
{
books b[size];
load(b, size);
print(b, size);
sort(b, size);
system("pause");
}
If you want to print the sorted list, you need to call sort before calling print:
void main()
{
books b[size];
load(b, size);
sort(b, size);
print(b, size);
system("pause");
}
Also, I think you need to define the books struct as
struct books b[size];
if you want to avoid compiler errors.
Finally, to print the list from low to high rather than high to low you can either modify the sorting algorithm as suggested in the another answer, or you can modify the printing algorithm as below:
void print(struct books b[], int n)
{
int i;
for (i = n-1; i>0; i--)
{
printf("Profit is:%d\n",b[i].profit);
}
}
Use something like this(inverted bubble sort):
void inverted_sort(books b[], int size){
int profit;
bool swap;
do{
swap = false;
for (int i= 0; i < (size - 1); i++){
if (b[i].profit < b[i + 1].profit){
profit = b[i].profit;
b[i].profit = b[i + 1].profit;
b[i + 1].profit = profit;
swap = true;
}
}
} while (swap);
}
And remember to change the functions order, inverted_sort() must go before print().
void main()
{
books b[size];
load(b, size);
inverted_sort(b, size);
print(b, size);
}
Hope this helps!
use this :
void sort(struct books b[], int n)
{
int i; int j;
books t;
for (i = 0; i < n; i++)
for (j = i + 1; j < n ; j++)
if (b[j].profit > b[i].profit)
{
t = b[j];
b[j] = b[i];
b[i] = t;
}
}
Related
how to output data into new files g1 g2 g3 for each sorting method in c language
This is the code that i have done .. Might be a little messy but i somehow managed to make it work i think. I was wondering how to separately print the sorting output in each of their different files. Any advice or suggestions on how to do it will be appreciated
edit: i dont know if the program works because the plan was to run the loop till the array size (which will be the total values in the file) but i didnt know how to run a loop through an array without knowing its size in c
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <time.h>
void getRandomArray(int arr[], int n, int max); //initiate an array with random numbers
void cloneArray(int arrB[], int arrA[], int size); //copy an array to another
void printArray(int arr[], int size); //print all elements of an array
void swap(int *xp, int *yp);
void selectionSort(int arr[], int n); //selection sort
void insertionSort(int list[], int n); //insertion sort
int main(int argc, char *argv[]) {
FILE *myFile;
myFile = fopen("test_dat.txt", "r");
//read file into array
int i;
int SIZE=10000;
int arrA[SIZE], arrB[SIZE];
if (myFile == NULL){
printf("Error Reading File\n");
exit (0);
}
for (i = 0; i <SIZE; i++){
fscanf(myFile, "%d,", &arrA[i] );
}
cloneArray( arrB, arrA, SIZE);
printArray(arrA, SIZE);
clock_t start = clock();
cloneArray(arrB, arrA, SIZE);
start = clock();
selectionSort(arrB, SIZE);
printf("Time = %f ms\n", 1000.0*(clock()-start)/CLOCKS_PER_SEC);
printArray(arrB, SIZE);
cloneArray(arrB, arrA, SIZE);
start = clock();
insertionSort(arrB, SIZE);
printf("Time = %f ms\n", 1000.0*(clock()-start)/CLOCKS_PER_SEC);
printArray(arrB, SIZE);
free(arrA);
free(arrB);
fclose(myFile);
}
//=======================================================
void cloneArray(int arrB[], int arrA[], int size) {
for (int i = 0; i < size; i++)
arrB[i] = arrA[i];
}
void printArray(int arr[], int size) {
for (int i=0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}
void swap(int *xp, int *yp) {
int temp = *xp;
*xp = *yp;
*yp = temp;
}
//=======================================================
void selectionSort(int arr[], int n) {
puts("\n==================== Selection Sort ==================== ");
int comp = 0, swp = 0;
int min_idx;
for (int i = 0; i < n-1; i++) {
min_idx = i;
for (int j = i+1; j < n; j++)
if (arr[j] < arr[min_idx]) {
min_idx = j;
comp++;
}
swap(&arr[min_idx], &arr[i]);
swp++;
}
printf("No. comparisons = %d; No. swaps = %d\n", comp, swp);
}
void insertionSort(int list[], int n) {
puts("\n==================== Insertion Sort ====================");
int comp = 0, assg = 0;
for (int h = 1; h < n; h++) {
int key = list[h];
int k = h - 1; //start comparing with previous item
while (k >= 0 && key < list[k]) {
list[k + 1] = list[k];
comp++;
assg++;
--k;
}
list[k + 1] = key;
}
printf("No. comparisons = %d; No. assignments = %d\n", comp, assg);
return 0;
} //end insertionSort
#define stdMax 5
#define SWAP(a,b) {int t; t = a; a=b; b=t;}
typedef struct student {
char name[100];
int stdNum;
} Student;
void bubbleSort(int std[], int n );
int main() {
char text[stdMax][100];
Student std[stdMax];
char* temp = NULL;
for (int i = 0; i < stdMax; i++) {
printf("%d Enter student name / student number: ", i + 1);
gets(&text[i][0]);
}
for (int i = 0; i < stdMax; i++ ) {
strcpy_s(std[i].name, 100, strtok_s(&text[i][0], "/",&temp));
std[i].stdNum = atoi(strtok_s(NULL,"/",&temp));
}
bubbleSort(std, 5);
for (int i = 0; i < stdMax; i++) {
printf("%d %s\n", std[i].stdNum, std[i].name);
}
}
void bubbleSort(int *std, int n)
{
int i, j;
for (i = n; i > 1; i--)
{
for (j = 1; j < i; j++)
{
if (std[j - 1] > std[j])
{
SWAP(std[j - 1], std[j]);
}
}
}
}
ex) student/1234,
I'd like to put 5 in order of number size. However, when this code is executed, it only outputs as it is entered. I don't know which part is wrong. I think there's something wrong with the bubble sort, so I'd appreciate it if you could help me.
You are dealing with an array of Student, but the bubbleSort function is for sorting int. Mismatch here.
#define stdMax 5
#define SWAP(a,b) {Student t; t = a; a=b; b=t;}
typedef struct student {
char name[100];
int stdNum;
} Student;
void bubbleSort(Student std[], int n );
/* omit: same as original */
void bubbleSort(Student *std, int n)
{
int i, j;
for (i = n; i > 1; i--)
{
for (j = 1; j < i; j++)
{
if (std[j - 1].stdNum > std[j].stdNum)
{
SWAP(std[j - 1], std[j]);
}
}
}
}
I tried to build a heap and finally print the elements in the form of an array.
Here it is the code (I know this doesn't really make sense but I just wanted to test my knowlwdge of heap and dynamic arrays):
#include <stdio.h>
#include <stdlib.h>
void heapiify(int *arr,int n, int i)
{
int largest=i;
int l=2*i+1; // left node
int r= 2*i+2; // right node
if(l<=n && *arr[l]>=*arr[i])
largest=l;
if (r <=n && *arr[r]<=*arr[i])
largest= r;
if(largest !=i)
{
int temp=*arr[i];
*arr[i]=*arr[largest];
*arr[largest]=temp;
}
heapify(*arr,n,largest);
}
void buildh(int *arr,int n,int r,int c)
{
int i;
for(i=n/2-1;i>=0;i--)
heapify(*arr,n,i);
output(*arr,r,c);
}
void output(int *arr,int r,int c)
{
int i,j;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d",*arr[i*c+j]);
}
printf("\n");
}
}
int main()
{
int i,j,r,c;
printf("enter the number of rows");
scanf("%d",&r);
printf("enter the number of columns");
scanf("%d",&c);
int n=r*c;
int *arr=malloc(n*sizeof(int));
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
scanf("%d",&arr[i*c+j]);
}
buildh(*arr,n,r,c);
}
I'm getting 9 errors which are all the same
invalid argument type of unary '*'( have int)
Your arr variable is of type pointer to int:
int *arr=malloc(n*sizeof(int));
So when you call buildh, which takes the same type, you have to pass it as-is:
buildh(arr,n,r,c);
Same for the other cases.
The problem is the dereference of arr, across your funtions in multiple places, and the passing of dereferenced *arr in your functions to int * parameters, you should pass arr, try:
//...
void heapify(int *arr, int n, int i)
{
int largest = i;
int l = 2 * i + 1; // left node
int r = 2 * i + 2; // right node
if (l <= n && arr[l] >= arr[i]) //here
largest = l;
if (r <= n && arr[r] <= arr[i]) //here
largest = r;
if (largest != i)
{
int temp = arr[i]; //here
arr[i] = arr[largest]; //here
arr[largest] = temp; //here
}
heapify(arr, n, largest); //here
}
void buildh(int *arr, int n, int r, int c)
{
int i;
for (i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i); //here
output(arr, r, c); //here
}
void output(int *arr, int r, int c)
{
int i, j;
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
printf("%d", arr[i * c + j]); //here
}
printf("\n");
}
}
int main()
{
//...
buildh(arr, n, r, c); //here
}
I coded as below to print all the permutations of three number :1,2,3.
But the output is:
1,1,1
1,1,2
1,1,3
1,2,1
1,2,2
1,2,3
The code is as follows:
#include<stdio.h>
#include<conio.h>
void perm(int);
int a[10],l=2;
int main()
{
int k;
k=0;
perm(k);
getch();
return 0;
}
void perm(int k)
{
int i;
for(a[k]=1;a[k]<=3;a[k]++)
{
if(k==2)
{
for(i=0;i<3;i++)
{
printf("%d ",a[i]);
}
printf("\n");
}
else
{
k++;
perm(k);
}
}
}
Please give the correct code.
Why do you increment k? k should not change for a given call to perm().
Also it's a bit too bad to be stuck with 3 permutations, you can easily generalize this way:
#include<stdio.h>
#include<conio.h>
static void perm(int, int);
static void all_perm(int);
int a[10];
int main()
{
all_perm(3);
getch();
return 0;
}
void all_perm(int n)
{
perm(0, n);
}
void perm(int k, int n)
{
if (k == n)
{
for(int i = 0; i < n; i++)
{
printf("%d ", a[i]);
}
printf("\n");
}
else
{
for(a[k]=1; a[k] <= n; a[k]++)
{
perm(k + 1, n);
}
}
}
Edit: Well, what you name permutations are not permutations.
The logic that I have used is to some extent similar to yours.
I have included the entire code to make it clear.
#include <stdio.h>
void recn(int*,int,int);
void print_arr(int*,int);
void main()
{
int arr[3] = {1,2,3};
recn(arr,3,0);
}
void print_arr(int *arr, int n){
int i;
for(i = 0,printf("\n"); i < n; printf("%d",arr[i++]));
}
void recn(int *arr, int n, int l) {
int i, j, f, k, xx = 0;
static int tst[15], a[14]={0};
if (l == n) {
for (i = 0; i < n; i++) {
tst[i] = arr[a[i]];
}
print_arr(tst,n);
return;
}
for (i = 0; i < n; i++) {
f = 0;
for (j = 0; j < l; j++)
if (a[j] == i)
f = 1;
if (!f) {
a[l] = i;
recn(arr, n, l + 1);
}
}
}
I want to a fill a small 2 d array with arbitrary values before moving onto a bigger array. However, when I compile and run my program, I get some weird output. It is not perfectly square. if someone could point out what im doing wrong that would be great.
void startarray(char (*arr)[10], int y_length, int x_length);
void printarray(char (*arr)[10], int y_length);
int main()
{
char arr[10][10];
startarray(arr, 10,10);
printarray(arr, 10);
return 0;
}
void startarray(char (*arr)[10], int y_length, int x_length)
{
int i;
int j;
for(i = 0; i <= y_length; i++)
{
for(j = 0; j < x_length; j++)
{
arr[i][j] = 'a';
}//end for
arr[i][j] = '\0';
}//end for
}
void printarray(char (*arr)[10], int y_length)
{
int i = 0;
while(i < y_length)
{
printf("\n%s", arr[i]);
i++;
}//end while
}