Anonymous
Anonymous
you never change strp or tp, hence your loop will run forever
ahhh i added a strp++ and tp++ so the loop doesnt run infinitely but the output still isnt what i want ;__; ��� this was the output lol cant even tell what it is XD
olli
ahhh i added a strp++ and tp++ so the loop doesnt run infinitely but the output still isnt what i want ;__; ��� this was the output lol cant even tell what it is XD
yes - there are more issues with your code. Now you increment i up to the length of str so it will be out of bounds for t. while (*tp != '\0') { if (str[i] != t[i]) ^^^^ { strcat( t1, str ); } } You probably want to change the comparison to use *strp and *tp. However, before the loop you need to make sure to 'reset' tp to the beginning of the array. Also, not sure whether you want to strcat the whole string all the time.
Yash
#include <assert.h> #include <ctype.h> #include <limits.h> #include <math.h> #include <stdbool.h> #include <stddef.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h> /* * Complete the 'equalStacks' function below. * * The function is expected to return an INTEGER. * The function accepts following parameters: * 1. INTEGER_ARRAY h1 * 2. INTEGER_ARRAY h2 * 3. INTEGER_ARRAY h3 */ void reverse(int [],int); int min(int,int,int); void sumArray(int [],int [],int); int search(int [],int,int,int); void readArray(int a[],int n); int equalStacks(int h1_count, int* h1, int h2_count, int* h2, int h3_count, int* h3); int main(){ int h1_count,h2_count,h3_count; scanf("%d %d %d",&h1_count,&h2_count,&h3_count); int h1[h1_count],h2[h2_count],h3[h3_count]; readArray(h1,h1_count); readArray(h2,h2_count); readArray(h3,h3_count); int res=equalStacks(h1_count,h1,h2_count,h2,h3_count,h3); printf("%d",res); } void readArray(int a[],int n){ for(int i=0;i<n;i++) scanf("%d",&a[i]); } void reverse(int h[],int n){ int i=0,j=n-1; while(i<j){ int t=h[i]; h[i]=h[j]; h[j]=t; i++;j--; } } int min(int a,int b,int c){ if(a<b){ if(a<c) return a; else return c; }else if(b<c) return b; else return c; } void sumArray(int h[],int s[],int n){ int sum=0; for(int i=0;i<n;i++){ sum+=h[i]; s[i]=sum; } } void copy(int a[],int b[],int n){ for(int i=0;i<n;i++) a[i]=b[i]; } int search(int a[],int i,int j,int x){ int c; if(i<=j){ c=i+j/2; if(a[c]==x) return 1; else if(a[c]>x) search(a,i,c-1,x); else search(a,c+1,j,x); } return 0; } int equalStacks(int h1_count, int* h1, int h2_count, int* h2, int h3_count, int* h3) { reverse(h1,h1_count); reverse(h2,h2_count); reverse(h3,h3_count); int sum1[h1_count],sum2[h2_count],sum3[h3_count],flag=1,n; sumArray(h1,sum1,h1_count); sumArray(h2,sum2,h2_count); sumArray(h3,sum3,h3_count); reverse(sum1,h1_count); reverse(sum2,h2_count); reverse(sum3,h3_count); if(min(h1_count,h2_count,h3_count)==h1_count){ // int arr[h1_count]; flag=1;n=h1_count; // copy(arr,h1,h1_count); }else if(min(h1_count,h2_count,h3_count)==h2_count){ // int arr[h2_count]; flag=2;n=h2_count; // copy(arr,h2,h2_count); }else { // int arr[h3_count]; flag=3;n=h3_count; // copy(arr,h3,h3_count); } for(int i=0;i<n;i++){ switch (flag) { case 1:if(search(sum2,0,h2_count,sum1[i])){ if(search(sum3,0,h3_count,sum1[i])) return sum1[i];}break; case 2:if(search(sum1,0,h1_count,sum2[i])){ if(search(sum3,0,h3_count,sum2[i])) return sum2[i];}break; default:if(search(sum2,0,h2_count,sum3[i])){ if(search(sum1,0,h1_count,sum3[i])) return sum3[i];}break; } } return 0; }
Yash
#include <assert.h> #include <ctype.h> #include <limits.h> #include <math.h> #include <stdbool.h> #include <stddef.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h> /* * Complete the 'equalStacks' function below. * * The function is expected to return an INTEGER. * The function accepts following parameters: * 1. INTEGER_ARRAY h1 * 2. INTEGER_ARRAY h2 * 3. INTEGER_ARRAY h3 */ void reverse(int [],int); int min(int,int,int); void sumArray(int [],int [],int); int search(int [],int,int,int); void readArray(int a[],int n); int equalStacks(int h1_count, int* h1, int h2_count, int* h2, int h3_count, int* h3); int main(){ int h1_count,h2_count,h3_count; scanf("%d %d %d",&h1_count,&h2_count,&h3_count); int h1[h1_count],h2[h2_count],h3[h3_count]; readArray(h1,h1_count); readArray(h2,h2_count); readArray(h3,h3_count); int res=equalStacks(h1_count,h1,h2_count,h2,h3_count,h3); printf("%d",res); } void readArray(int a[],int n){ for(int i=0;i<n;i++) scanf("%d",&a[i]); } void reverse(int h[],int n){ int i=0,j=n-1; while(i<j){ int t=h[i]; h[i]=h[j]; h[j]=t; i++;j--; } } int min(int a,int b,int c){ if(a<b){ if(a<c) return a; else return c; }else if(b<c) return b; else return c; } void sumArray(int h[],int s[],int n){ int sum=0; for(int i=0;i<n;i++){ sum+=h[i]; s[i]=sum; } } void copy(int a[],int b[],int n){ for(int i=0;i<n;i++) a[i]=b[i]; } int search(int a[],int i,int j,int x){ int c; if(i<=j){ c=i+j/2; if(a[c]==x) return 1; else if(a[c]>x) search(a,i,c-1,x); else search(a,c+1,j,x); } return 0; } int equalStacks(int h1_count, int* h1, int h2_count, int* h2, int h3_count, int* h3) { reverse(h1,h1_count); reverse(h2,h2_count); reverse(h3,h3_count); int sum1[h1_count],sum2[h2_count],sum3[h3_count],flag=1,n; sumArray(h1,sum1,h1_count); sumArray(h2,sum2,h2_count); sumArray(h3,sum3,h3_count); reverse(sum1,h1_count); reverse(sum2,h2_count); reverse(sum3,h3_count); if(min(h1_count,h2_count,h3_count)==h1_count){ // int arr[h1_count]; flag=1;n=h1_count; // copy(arr,h1,h1_count); }else if(min(h1_count,h2_count,h3_count)==h2_count){ // int arr[h2_count]; flag=2;n=h2_count; // copy(arr,h2,h2_count); }else { // int arr[h3_count]; flag=3;n=h3_count; // copy(arr,h3,h3_count); } for(int i=0;i<n;i++){ switch (flag) { case 1:if(search(sum2,0,h2_count,sum1[i])){ if(search(sum3,0,h3_count,sum1[i])) return sum1[i];}break; case 2:if(search(sum1,0,h1_count,sum2[i])){ if(search(sum3,0,h3_count,sum2[i])) return sum2[i];}break; default:if(search(sum2,0,h2_count,sum3[i])){ if(search(sum1,0,h1_count,sum3[i])) return sum3[i];}break; } } return 0; }
in this program i getting segmentation fault........i can't found where it is.......
Pavel
in this program i getting segmentation fault........i can't found where it is.......
Next time pls use pastebin or hastebin to share the code, it impossible to read it like this from mobile at least
Tangent Alpha
#include <assert.h> #include <ctype.h> #include <limits.h> #include <math.h> #include <stdbool.h> #include <stddef.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h> /* * Complete the 'equalStacks' function below. * * The function is expected to return an INTEGER. * The function accepts following parameters: * 1. INTEGER_ARRAY h1 * 2. INTEGER_ARRAY h2 * 3. INTEGER_ARRAY h3 */ void reverse(int [],int); int min(int,int,int); void sumArray(int [],int [],int); int search(int [],int,int,int); void readArray(int a[],int n); int equalStacks(int h1_count, int* h1, int h2_count, int* h2, int h3_count, int* h3); int main(){ int h1_count,h2_count,h3_count; scanf("%d %d %d",&h1_count,&h2_count,&h3_count); int h1[h1_count],h2[h2_count],h3[h3_count]; readArray(h1,h1_count); readArray(h2,h2_count); readArray(h3,h3_count); int res=equalStacks(h1_count,h1,h2_count,h2,h3_count,h3); printf("%d",res); } void readArray(int a[],int n){ for(int i=0;i<n;i++) scanf("%d",&a[i]); } void reverse(int h[],int n){ int i=0,j=n-1; while(i<j){ int t=h[i]; h[i]=h[j]; h[j]=t; i++;j--; } } int min(int a,int b,int c){ if(a<b){ if(a<c) return a; else return c; }else if(b<c) return b; else return c; } void sumArray(int h[],int s[],int n){ int sum=0; for(int i=0;i<n;i++){ sum+=h[i]; s[i]=sum; } } void copy(int a[],int b[],int n){ for(int i=0;i<n;i++) a[i]=b[i]; } int search(int a[],int i,int j,int x){ int c; if(i<=j){ c=i+j/2; if(a[c]==x) return 1; else if(a[c]>x) search(a,i,c-1,x); else search(a,c+1,j,x); } return 0; } int equalStacks(int h1_count, int* h1, int h2_count, int* h2, int h3_count, int* h3) { reverse(h1,h1_count); reverse(h2,h2_count); reverse(h3,h3_count); int sum1[h1_count],sum2[h2_count],sum3[h3_count],flag=1,n; sumArray(h1,sum1,h1_count); sumArray(h2,sum2,h2_count); sumArray(h3,sum3,h3_count); reverse(sum1,h1_count); reverse(sum2,h2_count); reverse(sum3,h3_count); if(min(h1_count,h2_count,h3_count)==h1_count){ // int arr[h1_count]; flag=1;n=h1_count; // copy(arr,h1,h1_count); }else if(min(h1_count,h2_count,h3_count)==h2_count){ // int arr[h2_count]; flag=2;n=h2_count; // copy(arr,h2,h2_count); }else { // int arr[h3_count]; flag=3;n=h3_count; // copy(arr,h3,h3_count); } for(int i=0;i<n;i++){ switch (flag) { case 1:if(search(sum2,0,h2_count,sum1[i])){ if(search(sum3,0,h3_count,sum1[i])) return sum1[i];}break; case 2:if(search(sum1,0,h1_count,sum2[i])){ if(search(sum3,0,h3_count,sum2[i])) return sum2[i];}break; default:if(search(sum2,0,h2_count,sum3[i])){ if(search(sum1,0,h1_count,sum3[i])) return sum3[i];}break; } } return 0; }
you forgot to put a return statement to the main function
Yash
you forgot to put a return statement to the main function
yes i did it later still it is giving same error
olli
strcat is for adding characters which arent a vowel to the new string so ig it should be there
yes, but strcat "appends a copy of the null-terminated byte string", you probably only want to add the character if it's not a vowel? (and not the whole string all the time) You could do something like this int IsVowel(int Character) { switch (Character) { case 'A': case 'a': case 'E': case 'e': case 'I': case 'i': case 'O': case 'o': case 'U': case 'u': return 1; default: return 0; } } void RemoveVowels(char * Input, char * Output) { if (!Output) { Output = Input; } for (; *Input; ++Input) { if (!IsVowel(*Input)) { *Output = *Input; ++Output; } } *Output = '\0'; }
Yash
i think segmentation related to memory allocation.........
Yash
but can't getting actual problem
Tangent Alpha
Yash
ok
Tangent Alpha
take a look at while loops and recursive functions
Tangent Alpha
it's hard to read the code
olli
#include <assert.h> #include <ctype.h> #include <limits.h> #include <math.h> #include <stdbool.h> #include <stddef.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h> /* * Complete the 'equalStacks' function below. * * The function is expected to return an INTEGER. * The function accepts following parameters: * 1. INTEGER_ARRAY h1 * 2. INTEGER_ARRAY h2 * 3. INTEGER_ARRAY h3 */ void reverse(int [],int); int min(int,int,int); void sumArray(int [],int [],int); int search(int [],int,int,int); void readArray(int a[],int n); int equalStacks(int h1_count, int* h1, int h2_count, int* h2, int h3_count, int* h3); int main(){ int h1_count,h2_count,h3_count; scanf("%d %d %d",&h1_count,&h2_count,&h3_count); int h1[h1_count],h2[h2_count],h3[h3_count]; readArray(h1,h1_count); readArray(h2,h2_count); readArray(h3,h3_count); int res=equalStacks(h1_count,h1,h2_count,h2,h3_count,h3); printf("%d",res); } void readArray(int a[],int n){ for(int i=0;i<n;i++) scanf("%d",&a[i]); } void reverse(int h[],int n){ int i=0,j=n-1; while(i<j){ int t=h[i]; h[i]=h[j]; h[j]=t; i++;j--; } } int min(int a,int b,int c){ if(a<b){ if(a<c) return a; else return c; }else if(b<c) return b; else return c; } void sumArray(int h[],int s[],int n){ int sum=0; for(int i=0;i<n;i++){ sum+=h[i]; s[i]=sum; } } void copy(int a[],int b[],int n){ for(int i=0;i<n;i++) a[i]=b[i]; } int search(int a[],int i,int j,int x){ int c; if(i<=j){ c=i+j/2; if(a[c]==x) return 1; else if(a[c]>x) search(a,i,c-1,x); else search(a,c+1,j,x); } return 0; } int equalStacks(int h1_count, int* h1, int h2_count, int* h2, int h3_count, int* h3) { reverse(h1,h1_count); reverse(h2,h2_count); reverse(h3,h3_count); int sum1[h1_count],sum2[h2_count],sum3[h3_count],flag=1,n; sumArray(h1,sum1,h1_count); sumArray(h2,sum2,h2_count); sumArray(h3,sum3,h3_count); reverse(sum1,h1_count); reverse(sum2,h2_count); reverse(sum3,h3_count); if(min(h1_count,h2_count,h3_count)==h1_count){ // int arr[h1_count]; flag=1;n=h1_count; // copy(arr,h1,h1_count); }else if(min(h1_count,h2_count,h3_count)==h2_count){ // int arr[h2_count]; flag=2;n=h2_count; // copy(arr,h2,h2_count); }else { // int arr[h3_count]; flag=3;n=h3_count; // copy(arr,h3,h3_count); } for(int i=0;i<n;i++){ switch (flag) { case 1:if(search(sum2,0,h2_count,sum1[i])){ if(search(sum3,0,h3_count,sum1[i])) return sum1[i];}break; case 2:if(search(sum1,0,h1_count,sum2[i])){ if(search(sum3,0,h3_count,sum2[i])) return sum2[i];}break; default:if(search(sum2,0,h2_count,sum3[i])){ if(search(sum1,0,h1_count,sum3[i])) return sum3[i];}break; } } return 0; }
what's your input? what's the expected output? depending on your size you might run out of stack space, hence don't use VLA's :)
Anonymous
Ohhhhh ohkayyyyyy i guess i know where i am wrong :D
Anonymous
nvm ;__; failed lol
Yash
take a look at while loops and recursive functions
in search function i didn't mention i+j in parenthesis ..........becoz / has more precedance than +
Yash
😅
Tangent Alpha
ok
#include <assert.h> #include <ctype.h> #include <limits.h> #include <math.h> #include <stdbool.h> #include <stddef.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h> /* * Complete the 'equalStacks' function below. * * The function is expected to return an INTEGER. * The function accepts following parameters: * 1. INTEGER_ARRAY h1 * 2. INTEGER_ARRAY h2 * 3. INTEGER_ARRAY h3 */ void reverse(int [], int); int min(int, int, int); void sumArray(int [], int [], int); int search(int [], int, int, int); void readArray(int a[], int n); int equalStacks(int h1_count, int* h1, int h2_count, int* h2, int h3_count, int* h3); int main(){ int h1_count, h2_count, h3_count; scanf("%d %d %d", &h1_count, &h2_count, &h3_count); int h1[h1_count], h2[h2_count], h3[h3_count]; readArray(h1, h1_count); readArray(h2, h2_count); readArray(h3, h3_count); int res = equalStacks(h1_count, h1, h2_count, h2, h3_count, h3); printf("%d",res); return 0; } void readArray(int a[],int n){ for(int i=0;i<n;i++) scanf("%d",&a[i]); } void reverse(int h[],int n){ int i=0,j=n-1; while(i<j){ int t=h[i]; h[i]=h[j]; h[j]=t; i++; j--; } } int min(int a,int b,int c){ if(a < b){ if(a < c) return a; else return c; } else if(b < c) return b; else return c; } void sumArray(int h[],int s[],int n){ int sum = 0; for(int i = 0; i < n; i++){ sum += h[i]; s[i] = sum; } } void copy(int a[],int b[],int n){ for(int i = 0; i < n; i++) a[i] = b[i]; } int search(int a[],int i,int j,int x){ int c; if(i <= j){ c = (i + j) / 2; if(a[c] == x) return 1; else if(a[c] > x) search(a, i, c-1, x); else search(a, c+1, j, x); } return 0; } int equalStacks(int h1_count, int* h1, int h2_count, int* h2, int h3_count, int* h3) { reverse(h1, h1_count); reverse(h2, h2_count); reverse(h3, h3_count); int sum1[h1_count], sum2[h2_count], sum3[h3_count], flag = 1, n; sumArray(h1, sum1, h1_count); sumArray(h2, sum2, h2_count); sumArray(h3, sum3, h3_count); reverse(sum1, h1_count); reverse(sum2, h2_count); reverse(sum3, h3_count); if(min(h1_count,h2_count,h3_count) == h1_count){ // int arr[h1_count]; flag = 1; n = h1_count; // copy(arr,h1,h1_count); } else if(min(h1_count,h2_count,h3_count) == h2_count){ // int arr[h2_count]; flag = 2; n = h2_count; // copy(arr,h2,h2_count); } else { // int arr[h3_count]; flag = 3; n = h3_count; // copy(arr,h3,h3_count); } for(int i = 0; i < n; i++){ switch (flag) { case 1: if(search(sum2, 0, h2_count, sum1[i])){ if(search(sum3, 0, h3_count, sum1[i])) return sum1[i]; } break; case 2: if(search(sum1,0,h1_count,sum2[i])){ if(search(sum3,0,h3_count,sum2[i])) return sum2[i]; } break; default: if(search(sum2,0,h2_count,sum3[i])){ if(search(sum1,0,h1_count,sum3[i])) return sum3[i]; } break; } } return 0; }
Tangent Alpha
this should work
Anonymous
how can I understand this sentence : "As one computer preliterate once commented, the price of liberty is eternal vigilance."
Anonymous
the former half sentence
Pavel
how can I understand this sentence : "As one computer preliterate once commented, the price of liberty is eternal vigilance."
Found this https://www.enotes.com/homework-help/eternal-vigilance-price-liberty-what-does-this-282826
Pavel
Ah, you mean "computer preliterate"?
Anonymous
yeah
Pavel
I always forget what former means 😂
Pavel
Like someone who existed before computers I guess. Preliterate is like existed before written languages, then computer preliterate probably means before computers 🤷‍♂ At least it how I understood it
Aakash
#include <stdio.h> #include <string.h> int main() { int i, j; char str[]= "AHAHAHHAHAHA BISHHHH I AM GONNA KEEL YOUU WITH EVERYTHING IN MOI ESSS"; char t[] = "AaEeIiOoUu"; char t1[69]; char *strp, *tp; strp = str; tp = t; while (*strp != '\0') { while (*tp != '\0') { if (str[i] != t[i]) { strcat( t1, str ); } } i++; } for (i=0 ; i<69 ; i++) { printf("%c", t1[i]); } printf("\n"); return 0; }
//always initialize to 0 i=0; while (*strp != '\0') { while (*tp != '\0') { if (*strp == *tp) { //you need to substitute in array not strcat() as string. Read strcat() manual. list[i] = *strp; } //this is where you increment *tp tp++; } //reset your *tp to start; by re-referencing. tp = &t; //this is where you increment *strp strp++; i++; } 🤪🤪
L3GACY
Anonymous
Anonymous
Vimal
/notes
Madhavan
/notes
Madhavan
/notes cpp
K M GANDHI
/notes
K M GANDHI
#india
Anonymous
#ide
K M GANDHI
#best-book
K M GANDHI
#best-book
K M GANDHI
#learn
K M GANDHI
#ides
Mahsun
/get - goodgoogling
K M GANDHI
#freeprogrammingbooks
K M GANDHI
#goodgoogling
K M GANDHI
#howtoprogram
Tangent Alpha
In addition, hackerrank and stuff helps
Oladoja
Compilers
Anonymous
Hello guys. Can anyone write the horse saddle point code in the matrix?
Anonymous
🖥🖥🖥🖥
Pavel
🖥🖥🖥🖥
/report spam
Nameful
🖥🖥🖥🖥
> most realty
Anonymous
Project Euler is a maths nightmare 😬 took me 12 hrs to just solve 6 problems under the category easy ..unless you know the formulas , you might find yourself bruteforcing almost every problem..
Anonymous
Hello guys. Can anyone write the horse saddle point code in the matrix?
The horse saddle point in the matrix is ​​defined as if a member is the maximum in row, is minimum in column, or vice versa
Anonymous
I observed all c++ typing modes but I don't understand how to make any software
Anonymous
Pavel
I observed all c++ typing modes but I don't understand how to make any software
Pick a book or watch youtube tutorials for beginners. If you have specific questions, google them. If you can't find the answer in google, ask here And not only read but test what you've learned (try to write some stuff) https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list
Anonymous
Some body Have you heard GRUB4DOS 0.4.6a This is error in any operating system I'm going to say it's been 6 months since I got into this too
Anonymous
Some body have any solutions to solve this
Anonymous
bootloader
tartaerae
Hi guys does anyone may help me to make a program in C? I have the algorithm already and it is very difficult to write it in c ..
Anonymous
can any one help me in understanding pointers
Anonymous
Hello, does anyone know how many libraries the Java language has?
olli
Hello, does anyone know how many libraries the Java language has?
That's unrelated to C/C++, but as in C++ anyone can create their own, so it's hard to count them
Igor🇺🇦