Anonymous
👍
Anonymous
Hi guys
Anonymous
Ok
Anonymous
Hi
Ravi
I wanna learn a programming language any buddy help me
dj
On c is there something where i could return element by element to calling funcrion and then come back to the original function
dj
like yield on python
i got a geeks for geeks link for that but cant send here 😅😁
RJ
i got a geeks for geeks link for that but cant send here 😅😁
Yeah I know python's yield, but i don't think C has something like that
kaneki
On c is there something where i could return element by element to calling funcrion and then come back to the original function
Afaik you don't have anything like that! You have to return it as a whole array... Or you can write a subroutine that you will call at the end of function and then call the parent function in that function
Anonymous
/warn very bad screen photo
din va lu
Help me
Anonymous
Help me
read the rules
Anonymous
qw34]\
Artöm
Is env valid?
dj
#include<stdio.h> int sum(int arr[], int n) { int sum = 0; // initialize sum // Iterate through all elements // and add them to sum for (int i = 0; i < n; i++) sum += arr[i]; return sum; } void main() { int i=0, j=0; int a[20]; char temp; do { scanf("%d%c", &a[i], &temp); i++; } while(temp != '\n'); int b=sum(a,13); printf("%d",b); } i am reading input from user and trying to find their sum but there is some problem while passing the array the values whoe sum is finding is not what i pass if the input i am giving is 1 2 3 4 sum is some random value
Alex
int b=sum(a,13); use i instead of 13
dj
https://stackoverflow.com/questions/3764014/how-do-you-read-scanf-until-eof-in-c
i=0 while(scanf("%d", &words) != EOF) { arr[i]=words; i++; } so does this work
dj
int b=sum(a,13); use i instead of 13
i gave 13 because just to try my input have 13 numbers
Alex
use i, not —i
dj
which one bro first or second
dj
which code
Shivang
Hi I m not able to compile my c++ file on vs code. If some one can help me out in this
Alex
sorry, telepaths are on vacation
Alex
I am not telepath
Alex
you should provide mistake, code, command
Alex
maybe someone will help
Alex
Anonymous
The Curious Cat
#india
dj
whats size_t bro i am a newbie :)
piggyho
you mean using gdb right.
gdb hint use gdb -tui ....
Sasuke
What's just happened
V01D
/get cbook
Anmol
If you dry run the code, it should be easy to understand. Try that
V01D
Dry run?
Immortal
please can someone help me with a source code for file transfer from C:drive to D;drive
Immortal
in Code:blocks C++
Immortal
console application
Anmol
Dry run?
Do everything with a pen and a paper exactly the way written in code
V01D
I was supposed to use links for code snippets. The pinned message stated pastebin as an option
Anonymous
Message from @V01D_NULL: Got a question about this code: (I have typos, when I referencd "num" I meant the "str" parameter in the convert function) https://pastebin.com/jHrPRpt2 It converts a numerical string to an integral data type. This is taken from a snippet on the internet, but I don't fully understand it. Can someone explain what the equation result = result * 10 + (str[i] - '0'); does?
V01D
Thanks
V01D
Do everything with a pen and a paper exactly the way written in code
I'm afraid I'm not grasping the equation. What am I missing here? Anything I should google for this?
Anmol
Suppose the string is 735
Anmol
Initially result is 0 Then 1st char of str is read which is 7. So res = 0 * 10 + 7 = 7 Then 2nd char of str is read which is 3. So res = 7 * 10 + 3 = 73
Anmol
This goes on.
Anmol
I think this is as basic as it gets
V01D
What about the - '0' though
V01D
And how does this convert a string to an integer
Anmol
Ohh wait. I think i misunderstood your question. Check ASCII values of characters for this
Anmol
int zero = '0' - '0'
Anmol
This typecasts the ASCII values of character to integer
V01D
So the equation basically takes an ascii char, and gets the ascii number for it.
MᏫᎻᎯᎷᎷᎬᎠ
And how does this convert a string to an integer
The one algorithm I know 1- Is to make a variable number where you'll store the converted integer 2- then iterate through the string of digits, each time identify the digit with a simple switch statement and add that number to declared number variable and then multiply number with 10 You'll get the result
Anmol
So the equation basically takes an ascii char, and gets the ascii number for it.
No. Suppose you want to convert char '3' to integer 3
Anmol
Ascii value of char 3 is 51 and ascii value of char 0 is 48
V01D
Kindof like this, right @bond00_7 ? int number; for (int i = 0; i < strlen(str); i++) { switch (i) { case "1": number = 1; number *= number; break; ... } return number; }
V01D
Right..
Anmol
So, int three = '3' - '0' = 51 - 48
MᏫᎻᎯᎷᎷᎬᎠ
Kindof like this, right @bond00_7 ? int number; for (int i = 0; i < strlen(str); i++) { switch (i) { case "1": number = 1; number *= number; break; ... } return number; }
int number = 0; for (int i = 0; i < strlen(str); i++) { switch (i) { case '1': number +=1; break; ... } number *= 10; } return number; } I bolded what you missed
V01D
Woops, yeah thanks. Is it bad to leave variables unitialized like at the top??
MᏫᎻᎯᎷᎷᎬᎠ
V01D
Sorry for the questions, I know how to program, but I don't know good practices
V01D
So rule of thumb is just to use 0 instead of undeclared
MᏫᎻᎯᎷᎷᎬᎠ
Swich case really is not suited to this
Do you have anything else?!
Anmol
Do you have anything else?!
int stringToInt(const string& num_str) { int num = 0; for (size_t i = 0; i < num_str.size(); i++) { if (num_str[i] < '0' || num_str[i] > '9') { cout << "Not a number" << '\n'; exit(0); } num = num * 10 + (num_str[i] - '0'); } return num; }
Anmol
I just wanted this thing to stop lol. Plus didn't wanna throw error and stuff to keep it simple