#include<stdio.h>
#include<conio.h>
void main(){
system("cls");
int k;
// printf("Enter a Number: ");
// scanf("%d",&n);
for(int i = 1; i<=5;i++){
k = 0;
for(int j = 1 , k+=i; j<=5; j++){
printf("%2d",k);
k += 5;
}
printf("\n");
}
getch();
}
this program in vs code not run but in turboo c this program run.In vs code give error:
error: expected '=', ',', ';', 'asm' or '__attribute__' before '+=' token
for(int j = 1 , k+=i; j<=5; j++){
This program in vs code not run, but in turbo c this program runs. In vs code it gives error:
error: expected '=', ',', ';', 'asm' or '__attribute__' before '+=' token
for(int j = 1 , k+=i; j<=5; j++){
GCC fails with this:
foo.c:12:26: error: invalid '+=' at end of declaration; did you mean '='?
for(int j = 1 , k+=i; j<=5; j++){
You can't use += in the first part of the for statement.
It's expecting a variable declaration, and you've given it two:
int j = 1, k += i;
This declaration of k hides the previous one, and you're attempting to initialize it in terms of itself.
It's unclear from your question what you're expecting to do with k, but try k = k + i or just k = i instead. Or just initialize k on the line above.
Related
I'm trying to print an inverted pyramid(or triangle) with *, but when I run the following code it skips the for loop for i=rows, i.e. doesn't print any *,
the second iteration goes smoothly. I assume the problem is within while loop but I just don't see it clearly.
Here is the code:
#include <stdio.h>
int main() {
int k, i, space, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
printf("\n");
for (i = rows; i >= 1; i--, k = 0) {
for (space = 1; space <= rows - i; space++) {
printf(" ");
}
printf("smth");
while (k < 2 * i - 1) {
printf("* ");
k++;
}
printf("\n");
}
return 0;
}
The program doesn't work as you expect because you have undefined behavior in the code, since you use the uninitialized variable k before it is initialized.
Uninitialized variables have an indeterminate value, and using them (except to initialize the variable) is undefined behavior.
You should initialize k before you use it, not only at the end of the outer loop. Something like
for(i=rows,k=0; i>=1; i--,k=0)
// ^^^^
// Added initial initialization
Or since you don't use k except in the while loop, why not turn it into a for loop? Like
for (int k = 0; k < 2 * i - 1; ++k)
printf("* ");
You only initialize k in the increment part of the for loop... Hence k is uninitialized for the first iteration. It happens to have a 0 or negative value, but its initial value is actually indeterminate: the program invokes undefined behavior.
If you compile with gcc -Wall -W -O2 or clang -Weverything -O2, the compiler will warn you about this.
You should always use standard idioms, it helps avoid such mistakes.
Here is an improved version:
#include <stdio.h>
int main(void) {
int k, i, space, rows;
printf("Enter the number of rows: ");
if (scanf("%d", &rows) != 1)
return 1;
printf("\n");
for (i = rows; i > 0; i--) {
for (space = 0; space < rows - i; space++) {
printf(" ");
}
printf("smth");
for (k = 0; k < 2 * i - 1; k++) {
printf("* ");
}
printf("\n");
}
return 0;
}
I was doing a Bingo - type project when i started to get some errors in my first "for" loop and in the end of the "bingo" structure.
Can someone tell me what have I done wrong?
struct bingo{
int table[5][5];
int i;
int j;
for (i = 0; i < 25; i++)
{
*(table + i) = rand()%75 + 1;
for (j = 0; j < 25; j++)
{
if ((j != i) && ((*(table + i)) == (*(table + j))))
{
i--;
j = 5 * 5;
}
}
}
};
errors:
1 IntelliSense: expected a type specifier ( first for)
2 IntelliSense: expected an identifier (end of the sturct)
Structs are only able to contain variables, they can't have any logic in them. In C++ you can have functions defined in structs, but even then you need a function header and body.
This is my simple for loop code:
for(int i=0; i < sisi ; i++)
{
for(int j=1; j <= sisi-i; j++)
{
if(j != sisi-i)
{
printf(" ");
}
else
{
for(int b=0; b < 2i+1; b++)
{
}
}
}
printf("\n");
}
The error is caught at line 6, it says 'error expected a ";"', but i think the code is ok and no wrong grammar inside the code... But why is it occured?
In your for loop condition 2i is invalid expressión.
It should be like this:
for(int b = 0; b < (2 * i) + 1; b++)
The error is this line:
for(int b=0;b<2i+1;b++)
If you wanted 2x i then use this:
int b;
for(b = 0; b < (2*i)+1; b++)
You can't multiply by putting a number next to a variable. It has to be 2 * i. You'll want to change 2i + 1 to 2 * i + 1.
I am compiling a small C program with Microsoft Visual Studio 2010 on Windows 7. Here's a small snippet:
void test(char[] s)
{
//some code here
}
But I am getting the following error:
Expected a ')'
How would I resolve this issue?
Actually the full code is here , which implement radix sort MSD in C (chapter 10 of this book):
#define N // integers to be sorted with values from 0 -256
void MSD (char[] s) {
msd_sort(s, 0, len(s), 0)
}
msd_sort(char [][] s, int lhs, int rhs, int d )
{
if (rhs<=lhs+1) return;
int * count =(int * )malloc(257*sizeof(int));
for(int i = 0; i < N; ++i)
count[s[i][d]+1]++;
for(int k = 1; k < 256; ++k)
count[k] += count[k-1];
for(int j = 0; j < N; ++j)
temp[count[s[i][d]]++] = a[i];
for(int i = 0; i < N; ++i)
s[i] = temp[i];
for(int i = 0; i<255;++i)
msd_sort(s, 1 + count[i], 1 + count[i+1], d+1);
}
void test(char s[]) {}
^^---brackets go here
What you wrote is Java.
Also :
don't forget to give a value to your #define N.
I see one malloc and no free, that's a memory leak.
Code1:
#include<stdio.h>
int main()
{
unsigned short i, j, k;
for (i = 0; i < 2; i++)
{
k = i * 4 + 4;
for (j = k - 4; j < k; j++)
printf("%hu ", j);
putchar('\n');
}
return 0;
}
Output of Code1:
0 1 2 3
4 5 6 7
Remarks of Code1:
Space after 3 and 7
Newline after 7 (Stackoverflow has removed it)
Code2:
#include<stdio.h>
int main()
{
unsigned short i, j, k;
for (i = 0; i < 2; i++)
{
k = i * 4 + 4;
for (j = k - 4; j < k; j++)
{
printf("%hu", j);
if (j + 1 != k) putchar(' ');
}
if (i + 1 != 2) putchar('\n');
}
return 0;
}
Output of Code2:
0 1 2 3
4 5 6 7
Remarks of Code2:
No space after 3 and 7
No newline after 7
Additional remark of Code2:
The problem of Code2 is that the algorithm always compares two values in the if blocks and so Code2 is not efficient. I want to use Code1 and change these:
Code3
#include<stdio.h>
int main()
{
unsigned short i, j, k;
for (i = 0; i < 2; i++)
{
k = i * 4 + 4;
for (j = k - 4; j < k; j++)
printf("%hu ", j);
printf("\b\n");
}
putchar('\b');
return 0;
}
These do not show the same output of Code2 because \b does not erase anything.
My question:
Is there any standard way to do so what I've tried in Code3?
Remark of my question:
I have searched the internet but have not determined the solution.
Edit the question if it is not clear.
Edit: I don't know why my question is not useful or constructive. Though the above is an arbitrary small example, but performance might be an issue when processing very large amount of data. I thought that the way of removing character from console output might improve performance and there might be a specific way to do so. That's why I've asked the question. I could write the following codes in the answers. Now I've known via comments that removing character from console output is not possible because it is implementation dependent.
The usual approach to this is to treat either the first or the last printf as a special case (outside of the loop):
for(ii=0; ii<2; ii++) {
jj = 0;
printf("%d", jj); // first number printed without space.
for(jj=1; jj<4; jj++) {
printf(" %d", jj); // include the space before the number printed
}
if(ii<2-1) printf("\n");
}
Obviously I simplified how the loops are constructed and what is printed - for simplicity. You could make the first printf statement
printf("\n%d", jj);
then you have a newline at the start of your output (often a good thing) and then you don't need the if statement later - you just don't have a newline printed at the end of the line (because it will be printed at the start...)
There are marginally more efficient ways of doing this that would involve no if statements at all - but these all come at the expense of less readable code. For example, here is a "no loop unrolling and no additional if statements" version of the code:
http://codepad.org/01qPPtee
#include <stdio.h>
int main(void) {
int ii, jj;
ii = 0;
while(1) {
jj = 0;
while(1) {
printf("%d", jj); // include the space before the number printed
jj++;
if(jj<4) printf("."); else break;
}
ii++;
if(ii<2) printf("*\n"); else break;
}
return 0;
}
Output:
0.1.2.3*
0.1.2.3
Basically I have taken the functionality of the for loop and made it explicit; I also use a . rather than a and "*\n" rather than "\n" to show in the printout that things behave as expected.
It does what you asked without extra evaluation of the condition. Is it more readable? Not really...
If it really bothers you, you can unroll your loops a little so that you treat the last item as a special case:
#include<stdio.h>
int main()
{
unsigned short i, j, k;
for (i = 0; i < 1; i++)
{
k = i * 4 + 4;
for (j = k - 4; j < k - 1; j++)
{
printf("%hu ", j);
}
printf("%hu\n", j);
}
k = i * 4 + 4;
for (j = k - 4; j < k - 1; j++)
{
printf("%hu ", j);
}
printf("%hu", j);
return 0;
}
#include <stdio.h>
int main(){
unsigned short num = 0, to=8;
while(num < to){
printf("%hu", num++);
if(num < to)
putchar(num % 4 == 0 ? '\n' : ' ');
}
#if 0
do{
printf("%hu", num++);
}while(num < to && putchar(num % 4 == 0 ? '\n' : ' '));
#endif
return 0 ;
}
Well, to try to answer your question, here's how I would do it:
for (i = k = 0; i < 2; i++){
if (i > 0) printf("\n");
for (j = 0; j < 4; j++, k++){
if (j > 0) printf(" ");
printf("%d", k);
}
}
I do it this way because I want to be sure every line but the first starts with a \n, and every item is separated by a space from the one before it.
Also, I do not want the row and column position to be intimately tied to the content of what is being printed.
In terms of performance, keep in mind that these if statements cost about 1 cycle, while each character printed costs at least hundreds if not thousands. printf goes through many layers of system calls to interpret its format string, build a buffer, send the buffer to the system I/O routines, which then cause repainting and scrolling of the console window. Get the idea?
DO NOT WORRY about performance unless you know you have a problem.
Then, don't guess. Use a diagnostic. Here's what I do.