I am trying to do a push, pop and list function into a stack. It should first push 4 strings(I write them as array because %s cannot run properly), and then pop 3 strings out, and list the last string. However, the code can't run properly. Can somebody helps me?
This is the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<conio.h>
#define MAX 4
int point = -1;
char item[MAX][20];
void push(char item[point])
{
point++;
};
void pop(char item[point])
{
point--;
};
void list()
{
if (point > -1) {
for (int i = 0; i <= point; i++) {
printf("%c\n", item[point-i]);
}
}
};
int main()
{
//AB01
char arrA[4] = {'A','B','0','1'};
for(int i=0;i<4;i++)
{
printf("%c",arrA[i]);
}
point++;
printf("\n");
push (arrA[4]);
//AB02
char arrB[4] = {'A','B','0','2'};
for(int i=0;i<4;i++)
{
printf("%c",arrB[i]);
}
point++;
push (arrB[4]);
printf("\n");
//AB03
char arrC[4] = {'A','B','0','3'};
for(int i=0;i<4;i++)
{
printf("%c",arrC[i]);
}
point++;
push (arrC[4]);
printf("\n");
//AB04
char arrD[4] = {'A','B','0','4'};
for(int i=0;i<4;i++)
{
printf("%c",arrD[i]);
}
point++;
push (arrD[4]);
printf("\n");
//Delete 3 strings
pop(arrA[point]);
pop(arrB[point]);
pop(arrC[point]);
list();
return 0;
}
It seems that the list cannot run properly. Because I'd used arrays to show the strings, and it makes me unable to list properly(because I used an array again in the list(), and then it becomes 2 array). What should I do if I want to let my strings(characters in array) being list?
Related
I just start to learn pointers to structures and I'm confused.I have to create a type of data ARRAY (which is associated with an array which contains integers.) like a structure which contains: numbers of array's elements and the array's elements stored in a part of memory(heap), dynamically allocated.
So I wrote:
typedef struct ARRAY
{
int nrElem; // number of elements
int *v[100];
};
Now I need to create 2 functions, one for reading an array from keyboard and the second one to display it using the structure I declared.
I tried but I get stuck.
void arrayDisplay(ARRAY *ps)
{
int i;
for(i=0;i<pd->nrElem;++i)
{
printf("%d",)
}
}
void readArray(ARRAY *ps)
{
int i;
for(i=0;i<pd->nrElem;++i)
{
printf("%d",)
scanf("%d",&);
}
}
How to continue?
Instead of an array of pointers int *v[100]; you need an array of ints int v[100]; in your data structure.
See code below:
#include <stdio.h>
#include <stdlib.h>
typedef struct ARRAY
{
int nrElem; // number of elements
int v[100];
} ARRAY;
void arrayDisplay(ARRAY *ps)
{
int i;
for(i=0;i<ps->nrElem;++i)
{
printf("%d\n", ps->v[i]);
}
}
void readArray(ARRAY *ps)
{
int i;
for(i=0;i<ps->nrElem;++i)
{
printf("%d: ", i);
scanf("%d",&ps->v[i]);
}
}
int main()
{
ARRAY a;
a.nrElem = 5;
readArray(&a);
arrayDisplay(&a);
return 0;
}
If you really want to use an array of int pointers you need to allocate the array first. And a different level of redirection for printf and scanf. But I'm not sure why you want to allocate memory for an integer array like this.
typedef struct ARRAY
{
int nrElem; // number of elements
int *v[100];
} ARRAY;
void arrayDisplay(ARRAY *ps)
{
int i;
for(i=0;i<ps->nrElem;++i)
{
printf("%d\n", *ps->v[i]);
}
}
void readArray(ARRAY *ps)
{
int i;
for(i=0;i<ps->nrElem;++i)
{
printf("%d: ", i);
scanf("%d",ps->v[i]);
}
}
int main()
{
ARRAY a;
int i;
a.nrElem = 5;
for(i=0;i<a.nrElem;++i) {
a.v[i] = (int*)malloc(sizeof(a.v[i]));
}
readArray(&a);
arrayDisplay(&a);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct kezdo
{
int mennyi;
char betu;
}KEZDO;
int main(int argc, char* argv[])
{
int j;
int i;
int db=0;
int volt=0;
char sajt[22];
FILE* f=fopen(argv[1], "r");
if(f==NULL)
{
fprintf(stderr, "Hiba a fajl megnyitasaban!");
}
int k = 20;
KEZDO t[k];
KEZDO tmp;
for(i=0;i<k;i++)
{
t[i].mennyi = 0;
}
while(fgets(sajt,22,f)!=0)
{
if(sajt[strlen(sajt)-1] == '\n')
{
sajt[strlen(sajt)-1] = '\0';
}
for(i=0;i<k;i++)
{
if(t[i].betu == toupper(sajt[0]))
{
t[i].mennyi++;
volt=1;
}
}
if(volt==0)
{
t[db].betu = toupper(sajt[0]);
t[db].mennyi++;
db++;
}
else
{
volt = 0;
}
}
for(i=0;i<db;i++)
{
printf("%c: %d\n", t[i].betu, t[i].mennyi);
}
return 0;
}
I tried strcmp and stricmp but neither worked. I tried to fully change the struct by sorting the struct properties. When the struct properties are sorted it doesn't work, but it worked before in a non-sorted order. What is preventing output when the struct properties are sorted?
As i can see in your code, you want to sort on char betu. One way to sort structures is via qsort but that'd require comparator function stated below:
int compare(const void *void_a, const void *void_b)
{
const KEZDO *a = void_a;
const KEZDO *b = void_b;
return (a->betu) < (b->betu);
}
//Perform sort like this;
qsort((void *) &t, db, sizeof(KEZDO) , compare );
Moreover, qsort is in #include <stdlib.h>
I am trying to make multithread array sorting program in c. But when I run the program, I get an "segmentation fault" error.
Can someone help?
What should I change? First array should be 300 the other should be 500. We sort sequences separately first. After that merge 2 sorted sequences. I use "gcc -pthreads -0 soru1 soru1.c" and "./soru1" commands.
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <time.h>
#define size 800
int orginal_dizi[size], dizi1[300], dizi2[500], dizi3[size];
int sayi,a=1;
int boyutbul(int *the)
{
int number=-1;
while(the[++number]!='\0'){}
return number;
}
void*runner(void *param)
{
int temp,i,k;
int *bolum = param;
sayi = boyutbul(bolum);
printf("\n----------unsorted %d. array-----------\n\n",a);
for(i=0; i<sayi;i++)
printf("%d\n", bolum[i]);
for(i=0; i<sayi;i++)
{
for(k=0; k<(sayi-i-1);k++)
{
if(bolum[k]>bolum[k+1])
{
temp=bolum[k];
bolum[k]=bolum[k+1];
bolum[k+1]=temp;
}
}
}
printf("\n----------sorted %d. array-----------\n\n",a);
for(i=0; i<sayi;i++)
printf("%d\n", bolum[i]);
a++;
pthread_exit(0);
}
int main()
{
pthread_t tid1,tid2, tid3;
int i=0;
while(i<size)
{
int yenisayi=1+rand()%1500;
int aynimi=0, j=0;
while(j<i)
{
if(orginal_dizi[j]==yenisayi)
{
aynimi=1;
break;
}
j++;
}
if(aynimi)
continue;
orginal_dizi[i]=yenisayi;
i++;
}
for(i=0;i<size;i++)
{
if(i<(300))
{
dizi1[i]=orginal_dizi[i];
}
else
{
dizi2[i-(500)-1]= orginal_dizi[i];
}
}
pthread_create(&tid1,NULL,runner,(void *)dizi1);
pthread_join(tid1,NULL);
pthread_create(&tid2,NULL,runner,(void *)dizi2);
pthread_join(tid2,NULL);
for(i=0; i<size;i++)
{
if(i<300)
{
dizi3[i]=dizi1[i];
}
else
{
dizi3[i]=dizi2[i-500];
}
}
pthread_create(&tid3,NULL,runner,(void *)dizi3);
pthread_join(tid3,NULL);
FILE *fp;
if((fp=fopen("son.txt","w"))== NULL)
printf("Dosya acilamadi.");
for(i=0;i<size;i++)
{
fprintf(fp, "%d\n", dizi3[i]);
}
fclose(fp);
return 0;
}
There is no multithreading in this program: every thread is immediately joined; noting runs in parallel.
The real problem is in boyutbul, which tries to figure out the length of the array by looking for '\0'. The way you initialize the arrays, there is no guarantee that they would have the terminating 0 (in fact, they wouldn't have any zeroes), so the program is doomed to access them beyond the bounds. This is UB.
#include<stdio.h>
#include<string.h>
void fill_A (char A[][10],int *pos1,int *pos2)
{
int j,k,l;
while (j=0)
{
k=0;
do{
printf("keep entering and terminate by end");
gets(A[k]); k++;}while(strcmp(A[k-1],"end")!=0);
*pos2=k-1;
for(l=0;l<k;l++)
{
if (strcmp(A[l],"middle")==0)
{
j=1; *pos1=l; break; }
}
}
}
void fill_B(char A[][10],char B[][10],int pos1)
{
int j,k;
for(j=0;j<pos1;j++)
{
strcpy(B[j],A[j]);
}
}
void fill_C(char A[][10],char C[][10],int pos1,int pos2)
{
int j;
for (j=pos1;j<pos2;j++)
{
strcpy(C[j],A[j]);
}
}
void print (char A[][10],int lim)
{
int j;
for (j=0;j<lim;j++)
{
puts(A[j]);
}
}
void main()
{
char A[100][10],B[100][10],C[100][10];
int pos1,pos2;
fill_A(A,&pos1,&pos2);
fill_B(A,B,pos1);
fill_C(A,C,pos1,pos2);
print (B,pos1);
print(C,pos2-pos1);
}
I am trying to:
1)Keep entering strings until terminated by user (entering end)
2) the list of strings should have a string "middle"
3) words before middle put in 2d string B
4) words between middle and end put in 2d string C.
Run: just a screen that I cannot type into.
Can you find the problem? Thank you!
after editing
the code is :
#include <stdio.h>
#include <string.h>
struct names//defining struct array with leinght of (3)
{
char firstname[50];
char lastname[50];
int age;
}n[4];
void show(struct names n[],int);//print the struct elements on the screen
void sortbyage(struct names n[],int);//sort the struct elements by age
int main(void)
{
struct names n[4];
int len=3;
strcpy(n[0].firstname,"david");
strcpy(n[0].lastname,"bekham");
n[0].age=18;
strcpy(n[1].firstname,"cristiano");
strcpy(n[1].lastname,"ronaldo");
n[1].age=20;
strcpy(n[2].firstname,"iron");
strcpy(n[2].lastname,"man");
n[2].age=16;
show(n,len);
sortbyage(n,len);
show(n,len);
return 0;
}
void show(struct names n[],int len)
{
for(int i=0;i<3;i++)
{
printf("%d-first name ==>%s\n",i+1,n[i].firstname);
printf("%d-last name ==>%s\n",i+1,n[i].lastname);
printf("%d-age==>%i\n",i+1,n[i].age);
}
}
void sortbyage(struct names n[],int len)
{
// int help=0;
for(int i=len-1;i>0;i--)
{
for(int j=0;j<i;j++)
{
if(n[j].age>n[j+1].age)
{
n[4]=n[j];
n[j]=n[j+1];
n[j+1]=n[4];
}
}
}
}
after Editing it works
note : in run time it types in the last line this message : * process returned -1073741819 *)) how could I fix this and does it a big problem or it's don't matter???
You need to access as follows. Please modify your code some thing as below
if(n[j].age > n[j+1].age)
Hope this helps.
Edit:
Please modify the sortbyage() function as follows and it is working fine.
void sortbyage(struct names n[],int len)
{
struct names temp;
for(int i=0;i<len;i++)
{
for(int j=i+1;j<len;j++)
{
if(n[i].age > n[j].age)
{
temp = n[i];
n[i] = n[j];
n[j] = temp;
}
}
}
}
Edit:
if(n[j].age>n[j+1].age)
{
help=n[j].age;
n[j].age=n[j+1].age;
n[j+1].age=help;
}