I need to make a sorting program specifically insertion sort where the data came from the operation file that I already made before, but the data from the operation file can't be passed to the sorting program which is affected the output too. I really appreciate you guys who were willing to help me, thank you.
#include <stdio.h>
#include <string.h>
int main
()
{
FILE *af = fopen("modul.txt", "r");
int i, n, j, key;
char id[6], eko[5], geo[5], sos[5], sej[5], pil;
printf("DATA NILAI UTBK SOSHUM 2019\n");
i=1;
do{
printf("\t\t | id | eko | geo | sos | sej |\n");
do{
fscanf(af, "%s %s %s %s %s", id, eko, geo, sos, sej);
printf("Data input ke-%d : |%s|-|%s|-|%s|-|%s|-|%s|\n", i, id, eko, geo, sos, sej);
i++;
}
while(!feof(af));
//mengurutkan data dengan metode INSERTION SHORT
printf("\nMengurutkan nilai TKA Soshum UTBK 2019\n");
printf("1. Nilai Ekonomi\n"); printf("2. Nilai Geografi\n"); printf("3. Nilai Sosiologi\n"); printf("4. Nilai Sejarah\n");
printf("Masukkan Pilihan : "); fflush(stdin); scanf("%c", &pil);
if (pil == '1')
{
printf("Nilai Ekonomi\n");
FILE *af = fopen("modul.txt", "r");
fscanf(af, "%s", eko[i]);
for (i=1; 1<=eko[i]; i++)
{
key = eko[i];
j = i-1;
while (j>=0 && eko[j]>key)
{
eko[j+1] = eko[j];
j = j-1;
}
eko[j+1] = key;
}
for (i=0; i<n; i++)
{
fscanf(af, "%s", eko[i]);
printf("%s", eko[i]);
}
printf("\n");
}
else if (pil == '2')
{
printf("2. Nilai Geografi\n");
for (i=1; 1<=n; i++)
{
key = geo[i];
j = i-1;
while (j>=0 && geo[j]>key)
{
geo[j+1] = geo[j];
j = j-1;
}
geo[j+1] = key;
}
for (i=0; i<n; i++)
{
printf("%d", geo[i]);
}
printf("\n");
}
else if (pil == '3')
{
printf("3. Nilai Sosiologi\n");
for (i=1; 1<=n; i++)
{
key = sos[i];
j = i-1;
while (j>=0 && sos[j]>key)
{
sos[j+1] = sos[j];
j = j-1;
}
sos[j+1] = key;
}
for (i=0; i<n; i++)
{
printf("%d", sos[i]);
}
printf("\n");
}
else if (pil == '4')
{
printf("4. Nilai Sejarah\n");
for (i=1; 1<=n; i++)
{
key = sej[i];
j = i-1;
while (j>=0 && sej[j]>key)
{
sej[j+1] = sej[j];
j = j-1;
}
sej[j+1] = key;
}
for (i=0; i<n; i++)
{
printf("%d", sej[i]);
}
printf("\n");
}
else
{
printf("Salah Masukkan Pilihan\nUlangi Lagi!");
}
}
while(!feof(af));
fclose(af);
}
all I need was how I can pass the data from the operation file to the sorting program
Related
I have an C exercice at school. I had to make a dynamically allocated matrix filled with random number and which have a size choosing by user. Then, the program ask to the user to redefine a bigger size, and the new matrix have to keep old values and fill new values with random.
The program is blocking when I'm trying to redefine the size of the matrix. I'm receiving a segmentation fault as of line 73. I dont understand why :|
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<stddef.h>
int nbRand() {
static int first = 0;
if(first == 0) {
srand(time(NULL));
first = 1;
}
return rand()%10;
}
int main(int argc, char *argv[] ){
int **matrice, **matrice2; // Tableau
int i, j; // Compteur
int longueur1, largeur1, longueur2, largeur2; // Taille dynamique tableau
size_t n;
printf("Saisissez la taille du tableau.\n");
printf("Longueur : ");
scanf("%d", &longueur1);
printf("Largeur : ");
scanf("%d", &largeur1);
matrice = (int**) malloc(longueur1*sizeof(int*));
for(i = 0; i < longueur1; i++) {
matrice[i] = (int*) malloc(largeur1*sizeof(int));
}
/* Autre manière
matrice = (int*) malloc(longueur1*largeur1*sizeof(int));
*/
for(i = 0; i < longueur1; i++) {
for(j = 0; j < largeur1; j++) {
matrice[i][j] = nbRand();
}
}
for(i = 0; i < longueur1; i++) {
for(j = 0; j < largeur1; j++) {
printf("%d ", matrice[i][j]);
}
printf("\n");
}
// Agrandir la matrice
printf("NOUVELLE TAILLE\n");
do {
printf("Longueur : ");
scanf("%d", &longueur2);
} while(longueur2 < longueur1);
do {
printf("Largeur : ");
scanf("%d", &largeur2);
} while(largeur2 < largeur1);
matrice2 = (int**) realloc(matrice, longueur2*sizeof(int*));
for(i = 0; i < longueur2; i++) {
matrice2[i] = (int*) realloc(matrice[i], largeur2*sizeof(int));
}
matrice=matrice2;
for(i = 0; i < longueur2; i++) {
for(j = 0; j < largeur2; j++) {
matrice[i][j] = nbRand();
printf("%d\t%d\t%d\n", i, j, matrice[i][j]);
}
}
printf("test\n");
for(i = 0; i < longueur2; i++) {
for(j = 0; j < largeur2; j++) {
printf("%d ", matrice[i][j]);
}
printf("\n");
}
}
EDITED :
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<stddef.h>
int nbRand() {
static int first = 0;
if(first == 0) {
srand(time(NULL));
first = 1;
}
return rand()%10;
}
int main(int argc, char *argv[] ){
int** matrice;
int** tmp; // Tableau
int i, j; // Compteur
int longueur1, largeur1, longueur2, largeur2; // Taille dynamique tableau
size_t n;
printf("Saisissez la taille du tableau.\n");
printf("Longueur : ");
scanf("%d", &longueur1);
printf("Largeur : ");
scanf("%d", &largeur1);
matrice = (int**) malloc(longueur1*sizeof(int*));
for(i = 0; i < longueur1; i++) {
*(matrice+i) = (int*) malloc(largeur1*sizeof(int));
}
for(i = 0; i < longueur1; i++) {
for(j = 0; j < largeur1; j++) {
matrice[i][j] = nbRand();
}
}
for(i = 0; i < longueur1; i++) {
for(j = 0; j < largeur1; j++) {
printf("%d ", matrice[i][j]);
}
printf("\n");
}
// Agrandir la matrice
printf("NOUVELLE TAILLE\n");
do {
printf("Longueur : ");
scanf("%d", &longueur2);
} while(longueur2 < longueur1);
do {
printf("Largeur : ");
scanf("%d", &largeur2);
} while(largeur2 < largeur1);
tmp = (int**) realloc(matrice, longueur2*sizeof(int*));
for(i = 0; i < longueur2; i++) {
*(tmp + i) = (int*) realloc(*(tmp+i), largeur2*sizeof(int));
}
matrice=tmp;
for(i = longueur1; i < longueur2; i++) {
for(j = largeur2; j < largeur2; j++) {
matrice[i][j] = nbRand();
// printf("%d\t%d\t%d\n", i, j, matrice[i][j]);
}
}
printf("test\n");
for(i = 0; i < longueur2; i++) {
for(j = 0; j < largeur2; j++) {
printf("%d ", matrice[i][j]);
}
printf("\n");
}
}
matrice2 = (int**) realloc(matrice, longueur2*sizeof(int*));
for(i = 0; i < longueur2; i++) {
matrice2[i] = (int*) realloc(matrice[i], largeur2*sizeof(int));
^^^^^^^^^^
|
-> You are accessing old pointer here.
}
is wrong, it should be
matrice2 = (int**) realloc(matrice, longueur2*sizeof(int*));
for(i = 0; i < longueur2; i++) {
matrice2[i] = realloc(matrice2[i], largeur2*sizeof(int));
}
The problem is to print this pattern:
5555555555
5444444445
5433333345
5432222345
5432112345
5432112345
5432222345
5433333345
5444444445
5555555555
This is my code:
#include<stdio.h>
int main()
{
int i,k,j,n,p;
printf("enter the no : ");
scanf("%d",&n);
p = n;
k = 0;
int a[2*n][2*n];
while (p>=1)
{
for(i=0+k;i<2*n-k;i++)
{
for(j=0+k;j<2*n-k;j++)
{
if(i == 2*n-k||i == k||j == k||j == 2*n-k)
{
a[i][j]=p;
}
else
{
a[i][j]= 8;
}
}
printf("\n");
}
k++,p--;
}
for(i=0;i<2*n;i++)
{
for(j=0;j<2*n;j++)
{
printf("%d",a[i][j]);
}
printf("\n");
}
return 0;
}
The result I get is:
5555555555
5444444448
5433333388
5432222888
5432118888
5432188888
5432888888
5438888888
5488888888
5888888888
Your code is overly complex. You should utilize the symmetry. Replace you while-loop with this code.
// Because of symmetry, both i and j can loop to n instead of 2*n
for(i=0; i<n; i++) {
p = n;
for(j=0; j<n; j++) {
// Assign four cells at once due to both horizontal and vertical symmetry
a[i][j] = a[i][2*n-j-1] = a[2*n-i-1][j] = a[2*n-i-1][2*n-j-1] = p;
if(j<i)
p--;
}
}
user input 10 data, after that, user input 1 number. and the program will print out the placement of where the number store in the array.
int main() {
int arr[10];
int data,num;
for (int i = 0; i < 10; i++) {
printf("%d ",i );
scanf("%d",&arr[i]);
}
printf("Input data : " );
scanf("%d",&data );
for (int j = 0; j < arr[data]; j++) {
if (data==arr[j]) {
printf("Data %d found in array %d\n",data,arr[j] );
break;
}
else {
printf("Data not found in array\n");
break;
}
}
return 0;
}
int main() {
int arr[10];
int data, flag = 0;
for (int i = 0; i < 10; i++) {
printf("%d ",i );
scanf("%d",arr[i]);
}
printf("Input data : " );
scanf("%d",&data );
for (int j = 0; j < 10; j++) {
if (data==arr[j]) {
printf("Data %d found in array at %dth position\n",data,j);
flag = 1;
break;
}
}
if(!flag){
printf("data not found in the array\n");
}
return 0;
}
In this code I try to create three arrays. With these three arrays I successflly tried to make the union. But when I want to make an Intersection and the difference I couldn't.
#include <stdio.h>
int main()
{
int dizi1Sinir, dizi1Deger; // ilk alt kumenin degiskenleri
int dizi2Sinir, dizi2Deger; // ikinci alt kumenin degiskenleri
int dizi3Sinir, dizi3Deger; // ucuncu alt kumenin degiskenleri
// BIRINCI ALT KUME TANIMLAMA ISLEMLERI
printf("ilk alt kumen kac elemanli olsun?\n");
scanf("%d", &dizi1Sinir);
int alt_kume_1[dizi1Sinir]; // ilk alt kume
for(dizi1Deger = 0; dizi1Deger < dizi1Sinir; dizi1Deger++)
{
printf("dizinin elemanlarini gir.\n %dnci eleman = ", dizi1Deger + 1);
scanf("%d", &alt_kume_1[dizi1Deger]);
}
// -------------------------------------------------------------------------------
//IKINCI ALT KUME TANIMLAMA ISLEMLERI
printf("ikinci alt kumen kac elemanlı olsun?\n");
scanf("%d", &dizi2Sinir);
int alt_kume_2[dizi2Sinir]; // ikinci alt kume
for(dizi2Deger = 0; dizi2Deger < dizi2Sinir; dizi2Deger++)
{
printf("dizinin elemanlarini gir.\n %dnci eleman = ", dizi2Deger + 1);
scanf("%d", &alt_kume_2[dizi2Deger]);
}
//UCUNCU ALT KUME TANIMLAMA ISLEMLERI
printf("ucuncu alt kumen kac elemanli olsun?\n");
scanf("%d", &dizi3Sinir);
int alt_kume_3[dizi3Sinir]; // ucuncu alt kume
for(dizi3Deger = 0; dizi3Deger < dizi3Sinir; dizi3Deger++)
{
printf("dizinin elemanlarini gir.\n %dnci eleman = ", dizi3Deger + 1);
scanf("%d", &alt_kume_3[dizi3Deger]);
}
//---------------------------------
int azami=dizi1Sinir+dizi2Sinir+dizi3Sinir;
int birlesim[azami];
int i;
for(i=0;i<azami;i++)
{
if(i<dizi1Sinir)
birlesim[i]=alt_kume_1[i];
else if(i-dizi1Sinir<dizi2Sinir)
birlesim[i]=alt_kume_2[i-dizi1Sinir];
else
birlesim[i]=alt_kume_3[i-dizi1Sinir-dizi2Sinir];
}
//-----------------------------
//Ayni Elemanlari Sil (-1)
for(i=0;i<azami;i++)
{
int tempSayi=birlesim[i];
int j;
for(j = 0;j<i;j++)
{
if(birlesim[j]==tempSayi)
{
birlesim[i]=-1;
}
}
}
printf("\nbirlesim = ");
for(i = 0; i < azami; i++)
{
if(birlesim[i]!=-1)
printf("%d ", birlesim[i]);
}
int main() {
//************************
printf("\ninsertion dizi_1 and dizi_2 : ");
intersection_two_arrays(alt_kume_1,dizi1Sinir,alt_kume_2,dizi2Sinir);
printf("\ninsertion dizi_1 and dizi_3 : ");
intersection_two_arrays(alt_kume_1,dizi1Sinir,alt_kume_3,dizi3Sinir);
printf("\ninsertion dizi_2 and dizi_3 : ");
intersection_two_arrays(alt_kume_2,dizi2Sinir,alt_kume_3,dizi3Sinir);
printf("\ninsertion dizi_1 dizi_2 and dizi_3 : ");
intersection_three_arrays(alt_kume_1,dizi1Sinir,alt_kume_2,dizi2Sinir,alt_kume_3,dizi3Sinir);
//...etc
//*************************
printf("\nDifference dizi_1 - dizi_2 : ");
differenceX_Y(alt_kume_1,dizi1Sinir,alt_kume_2,dizi2Sinir);
printf("\nDifference dizi_2 - dizi_1 : ");
differenceX_Y(alt_kume_2,dizi2Sinir,alt_kume_1,dizi1Sinir);
printf("\nDifference dizi_2 - dizi_3 : ");
differenceX_Y(alt_kume_2,dizi2Sinir,alt_kume_3,dizi3Sinir);
printf("\nDifference dizi_1 - dizi_2 and dizi_3 : ");
differenceX_YandZ(alt_kume_1,dizi1Sinir,alt_kume_2,dizi2Sinir,alt_kume_3,dizi3Sinir);
//...etc
//*************************
}
function 1 : Intersection of two sets
void intersection_two_arrays(int kume_1[], int sinir1 , int kume_2[] , int sinir2)
{
int i,j;
int value;
if(sinir1>sinir2) value=sinir2;
else value=sinir1;
int hold[value];
int h=0;
for(i=0; i<sinir1; i++) {
for(j=0; j<sinir2; j++) {
if(kume_1[i] == kume_2[j]) {
hold[h++] = kume_1[i];
continue;
}
}
}
if(h==0) {
printf("There is not common value");
}
else {
for(i=0; i<h; i++) {
printf("%d ", hold[i]);
}
}
}
function 2 : Intersection of three sets
void intersection_three_arrays(int kume_1[], int sinir1 , int kume_2[] , int sinir2,
int kume_3[], int sinir3)
{
int i,j;
int value;
if(sinir1>sinir2) value=sinir2;
else value=sinir1;
int hold[value];
int h=0;
for(i=0; i<sinir1; i++) {
for(j=0; j<sinir2; j++) {
if(kume_1[i] == kume_2[j]) {
hold[h++] = kume_1[i];
continue;
}
}
}
intersection_two_arrays(hold,h, kume_3,sinir3);
}
function 3 : Difference X-Y
void differenceX_Y(int kume_1[], int sinir1 , int kume_2[] , int sinir2)
{
int i,j;
int value;
if(sinir1>sinir2) value=sinir1;
else value=sinir2;
int hold[value];
int h=0;
int findEqual=0;
for(i=0; i<sinir1; i++) {
for(j=0; j<sinir2; j++) {
if(kume_1[i] == kume_2[j]) {
findEqual++;
continue;
}
}
if(findEqual==0) hold[h++] = kume_1[i];
findEqual=0;
}
if(h==0) {
printf("There is not different value.");
}
else {
for(i=0; i<h; i++) {
printf("%d ", hold[i]);
}
}
}
function 4 : Difference X-(Y and Z)
void differenceX_YandZ(int kume_1[], int sinir1 , int kume_2[] , int sinir2, int
kume_3[], int sinir3)
{
int i,j;
int value;
if(sinir1>sinir2) value=sinir1;
else value=sinir2;
int hold[value];
int h=0;
int findEqual=0;
for(i=0; i<sinir1; i++) {
for(j=0; j<sinir2; j++) {
if(kume_1[i] == kume_2[j]) {
findEqual++;
continue;
}
}
if(findEqual==0) hold[h++] = kume_1[i];
findEqual=0;
}
differenceX_Y(hold, h , kume_3, sinir3);
}
I am trying to rotate an array that looks like this:
a a a a
a b a a
b b b a
a a a a
I am supposed to rotate it 5 times for 90 degrees. It is supposed to be done in C.
I appreciate every help because I am just a beginner and am stuck on this.
Thanks in advance.
#include <stdio.h>
int main()
{
char array_1[4][4] = { {'-','-','-','-'},
{'-','o','-','-'},
{'o','o','o','-'},
{'-','-','-','-'}};
char array_2[4][4] = { {'-','-','-','-'},
{'-','o','o','-'},
{'o','o','-','-'},
{'-','-','-','-'}};
char array_3[4][4] = { {'-','-','-','-'},
{'-','o','-','-'},
{'-','o','-','-'},
{'-','o','o','-'}};
char array_4[4][4] = { {'-','-','o','-'},
{'-','-','o','-'},
{'-','-','o','-'},
{'-','-','o','-'}};
int counter = 0;
int counter_1 = 0;
for(counter = 0; counter < 4; counter++)
{
for(counter_1 = 0; counter_1 < 4; counter_1++)
{
printf("%c ",array_1[counter][counter_1]);
}
printf(" ");
for(counter_1 = 0; counter_1 < 4; counter_1++)
{
printf("%c ",array_2[counter][counter_1]);
}
printf(" ");
for(counter_1 = 0; counter_1 < 4; counter_1++)
{
printf("%c ",array_3[counter][counter_1]);
}
printf(" ");
for(counter_1 = 0; counter_1 < 4; counter_1++)
{
printf("%c ",array_4[counter][counter_1]);
}
printf(" ");
printf("\n");
}
printf("\n");
for(counter= 0; counter < 4; counter++)
{
for(counter_1 = 3; counter_1 >= 0; counter_1--)
{
printf("%c ",array_1[counter_1][counter]);
}
printf(" ");
for(counter_1 = 3; counter_1 >= 0; counter_1--)
{
printf("%c ",array_2[counter_1][counter]);
}
printf(" ");
for(counter_1 = 3; counter_1 >= 0; counter_1--)
{
printf("%c ",array_3[counter_1][counter]);
}
printf(" ");
for(counter_1 = 3; counter_1 >= 0; counter_1--)
{
printf("%c ",array_4[counter_1][counter]);
}
printf(" ");
printf("\n");
}
printf("\n");
like this:
#include <stdio.h>
typedef struct point { int x, y; } Point;
void rotate(int n, char array[n][n]){
//rotate right 90 degrees
if(n == 1) return ;
int times = n / 2;
for(int i = 0; i < times; ++i){
Point base = { i, i };
for(int j = 0; j < n - 1; ++j){
Point transition[4] = { {j, n-1}, {n-1,n-1-j},{n-1-j,0},{0,j} };
char curr = array[base.x][base.y+j];//base + {0,j}
for(int k = 0; k < 4; ++k){
char temp = array[base.x + transition[k].x][base.y + transition[k].y];
array[base.x + transition[k].x][base.y + transition[k].y] = curr;
curr = temp;
}
}
n -= 2;
}
}
void display(int n, char array[n][n]){
for(int i = 0; i < n; ++i){
for(int j = 0; j < n; ++j){
if(j)
putchar(' ');
putchar(array[i][j]);
}
putchar('\n');
}
putchar('\n');
}
int main(void){
//demo
char array4[4][4] = {
{'1','2','3','4'},
{'5','6','7','8'},
{'9','A','B','C'},
{'D','E','F','0'}
};
display(4, array4);
int n = 4;
while(n--){
rotate(4, array4);
display(4, array4);
}
char array5[5][5] = {
{'A','B','C','D','E'},
{'F','G','H','I','J'},
{'K','L','M','N','O'},
{'P','Q','R','S','T'},
{'U','V','W','X','Y'}
};
display(5, array5);
n = 4;
while(n--){
rotate(5, array5);
display(5, array5);
}
}