Anonymous
Arrays have fixed size nonetheless
Shvmtz
#include <stdio.h> #include <stdlib.h> #include <string.h> const int ARRAY_LENGTH = 5; // #define ARRAY_LENGTH 5 void printarray(int* myarray, int length) { for (int i =0; i < length; i++) printf("%i, ", myarray[i]); printf("\n"); } int main(int argc, char** argv) { int a[ARRAY_LENGTH] = { 1, 2, 3, 4, 5 }; int b[ARRAY_LENGTH] = { 5, 4, 3, 2, 1 }; int* p = malloc(sizeof(int) * 15); for (int i = 0; i < 15; i++) p[i] = i; int arglengths[argc]; for (int i = 0; i < argc; i++) arglengths[i] = strlen(argv[i]); p = realloc(p, sizeof(int) * 20); printarray(a, ARRAY_LENGTH); printarray(b, ARRAY_LENGTH); printarray(p, 20); printarray(arglengths, argc); free(p); }
Anonymous
Watch/ read about it.
Shvmtz
Shvmtz
You got any error ? Like.. error: variable-sized object may not be initialized
Shvmtz
Okay. So you initialized the size variable inside the main(). What if you initialize the SIZE variable(const) globally outside the main() and use that SIZE variable with array inside main() ?
Shvmtz
Yes. Maybe
Shvmtz
Can you share code ?
Newly
Hi guys this is regarding the Embedded C, can you please let me know. how to full load the internal clock.
Shvmtz
Okay
Shvmtz
I think. If you initialize the array while declaring it, like: int tab[siz]= {11, 22, 33}; This will produce some error.
Anonymous
how do i convert a pointer into a float, and then back into a pointer? eg TEX * tex; // ... tex = blah; floatArray.push_back({0, FLOAT_TRUE, tex}); // ... auto data = floatArray.back(); if (data.isTex == FLOAT_TRUE) { setTex((TEX*)data.tex); }
Egor
Muchas gracias...locura
Anonymous
Header file for all .....can anybody help me out
Anonymous
Its std byte ++ something...i forgot please help me anyone
Anonymous
Thankyou💗
Anonymous
Is there any disadvantage to use this ..?
Anonymous
Is there any disadvantage to use this ..?
It is most often a Precompiled header. But it is generally not a good idea to include header files that you dont need because it bloats up your code. People usually use this header file for competitive programming but it is not a standard header file and you wont find people using it in professional work.
Anonymous
Okay thankyou .❤️
Hanz
Hello, what is __LINE__ in C? i found it on a source in GitHub
Hanz
is that thing standard?
Hanz
ok i foudn it on the internet
Anonymous
Here's a program to find largest from 3 numbers, No syntax errors in the code, but it doesn't work properly. #include <stdio.h> #include <stdlib.h> int main() { int n1,n2,n3,max; printf(" Input 3 values you want to compare\n"); scanf("%d %d %d", &n1, &n2, &n3); if((n1>n2)&&(n1>n2)) max = n1; else if((n2>n1)&&(n2>n3)) max = n2; else max - n3; printf(" largest value is %d", max); return 0; }
Hanz
np
Anonymous
Hello, what is __LINE__ in C? i found it on a source in GitHub
They are just preprocessor macros. _ _LINE_ _ - Current line number _ _FILE_ _ - Current file name _ _TIME_ _ - The time at which the current file was compiled _ _DATE_ _ - The date the file was compiled _ _func_ _ - Name of the current function.
Anonymous
Write a c program to input an admission number, marks of a module and display admission number with grade. The grade is assigned as follows. Marks >=75 A Marks 65-75 B Marks 55-65 C Marks 45-55 S Marks <45 F #include <stdio.h> int main() { int adnum,marks; char grade; printf("Enter your admission number "); scanf("%d",&adnum); printf("Enter your marks "); scanf("%d",&marks); if (marks >= 75) grade == "A"; else if (marks >= 65) grade == "B"; else if (marks >= 55) grade == "C"; else if (marks >= 45) grade == "S"; else grade == "F"; printf("your admission number is %d and grade is %c",adnum,grade); return 0; } Grade isn't print correctly, anyone can solve it?
Kaddy
How do I take input from the standard input stream in the following manner? I want the user to be able to input any number of characters, even 50,000 on the terminal before pressing enter. The number of input characters isn't fixed at the start. I don't want to store the input string, I just want to keep a count of how many characters are entered
Kaddy
Use a istream_iterator
Could you elaborate please…
Anonymous
Could you elaborate please…
istream_iteratot<char> inp(cin), end; unsigned count = 0; while(inp != end){ *inp++; ++count; } //count is the number of characters entered. If you dont want to consider the enter key then break out of the while loop if the user entered '\n'
Anonymous
pastecode.io/s/r02i1175 So I have this recursive function that prints values from the given number till 1, decrementing by 1 each time. Is there any way I can make the function return a vector consisting of the values instead of printing them? I'm trying to avoid global variables.
Anonymous
//Write a program to input a character display the entered character is a vowel or not . #include <stdio.h> #include <stdlib.h> int main() { char c; printf("Enter a character \n"); scanf("%c", c); switch(c) { case 'a': printf(" %c is a vowel", c);break; case 'e': printf(" %c is a vowel", c);break; case 'o': printf(" %c is a vowel", c);break; case 'u': printf(" %c is a vowel", c);break; case 'i': printf(" %c is a vowel", c);break; default : printf(" %c is not a vowel", c); } }
Kaddy
Yes you could do that albeit with some more lines of code.
Oh could you tell how? The reason is that I wanna solve this problem with the most basic tools, avoiding use of iterators or STL functions if possible
Anonymous
Oh could you tell how? The reason is that I wanna solve this problem with the most basic tools, avoiding use of iterators or STL functions if possible
Just use getchar() till you read EOF or '\n' and break out of the loop and print the number of characters encountered so far.
Anonymous
Dont use recursion. Use a while loop or for loop instead. Just create a vector in your func and push back the values you need. When you are done return the vector.
The actual code is more complex and I have to use recursion. I was thinking of static variable but there's no way to clear the vector for further calls of the function.
Anonymous
Pass in a vector by reference then.
That might work. Thanks!
Kaddy
Just use getchar() till you read EOF or '\n' and break out of the loop and print the number of characters encountered so far.
Now here's the interesting part… I tried this and it surprisingly runs into an infinite loop. EOF is out of question since I'm reading from terminal from standard input stream. Now when I write a loop as follows: char c; while(c != '\n') { c = getchar(); len++; } This goes into an infinite loop. It will take your input char stream but then when you press enter, it doesn't do anything. You keep pressing enter and nothing happens. Dunno why it happens 🤔
Kaddy
Now that the doubt is cleared, just for conceptual clarity what exactly is the difference in way that cin and getchar() work for my question? If you replaced the while loop condition with while(cin >> c) an infinite loop follows so there's some fundamental difference in the way that cin and getchar() are operating… anyone knows how?
Anonymous
Now that the doubt is cleared, just for conceptual clarity what exactly is the difference in way that cin and getchar() work for my question? If you replaced the while loop condition with while(cin >> c) an infinite loop follows so there's some fundamental difference in the way that cin and getchar() are operating… anyone knows how?
while(cin>>c) tests the status of cin. If cin is in bad state, failed state or EOF state then this loop will terminate. It wont terminate as long as a valid input is read and that includes enter key presses. The while(getchar() != '\n') checks specifically that the key pressed is not the enter key. So you could do the same with cin as while((cin>>c) && c != '\n')
Hanz
no, any code you have worked on?
Ravi
https://pastebin.com/9PjW5RBD It is returning null LinkedList, why so?
Anonymous
https://pastebin.com/9PjW5RBD It is returning null LinkedList, why so?
<quote> head = temp; //head will be nullptr here for(auto i:arr)     {         temp = i;         temp = temp->next;     } </quote> This code is wrong. You want head to be the first node in the vector but instead you are setting head to nullptr. Moreover the code in the for loop is wrong. You want a node's next node to be the element following it in the vector but instead you are using what was the next node in one of the two sorted lists which is wrong.
Anonymous
And why are you even using a vector? Doesnt it defeat the purpose of using a linked list in the first place? The sorted list can be created in place without using the vector by instead just manipulating the next pointers.
Anonymous
What is the mechanism behind the rand() and srand() functions??
Anonymous
What is the mechanism behind the rand() and srand() functions??
You want to know how pseudo random number generators work?
Anonymous
Yes please!
Well there are many ways you can generate them. The simplest way would be a linear congruential generator. You start with some number say a seed. You could generate the first number by substituting seed for x in (a*x+b) mod m where a,b and m are decided by the generator. Typically m is a very large prime number and a and b are less than m. The next number is generated by substituting the previous generated number for x in the above equation. That is why when you start with the same seed, you get the same sequence of random numbers. This is the simplest RNG and there are much better PRNGs in use. rand() doesnt use a Linear Congruential Generator btw.
Anonymous
Overstood, now which is better either to use the built in functions or to define my own?
If you are asking the question how RNGs work then you should definitely use the library functions because that uses a uniform probability distribution to generate random numbers unlike the Linear Congruential Generator shown above.
Anonymous
C++ has a better support for random numbers btw (though it is lacking in many aspects still) as compared with C.
Official hooligan of Pius XII
g++, if you're on linux, it's probably installed already
Anonymous
wtf? error: cannot initialize return object of type 'VertexEngine::Triple<const char *, unsigned int, Diligent::ITexture *> *' with an lvalue of type 'VertexEngine::Triple<const char *, unsigned long, Diligent::ITexture *> *'
Anonymous
oh
Anonymous
rip my ide replaces size_t with unsigned long