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
RJ
dj
Nils
kaneki
Anonymous
/warn very bad screen photo
din va lu
Help me
Anonymous
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
dj
Alex
Alex
int b=sum(a,13);
use i instead of 13
Alex
dj
Alex
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 :)
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
Anmol
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
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ᏫᎻᎯᎷᎷᎬᎠ
MᏫᎻᎯᎷᎷᎬᎠ
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ᏫᎻᎯᎷᎷᎬᎠ
Anmol
MᏫᎻᎯᎷᎷᎬᎠ
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