Why in C without a semicolon gives an error? [closed] - c

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 months ago.
Improve this question
Why does it throw an error in the seventh round without semicolons (;). When do you need to put such semicolons in the code?
#include <stdio.h>
#define S 10
void print_arr(int *arr, int size) {
int *p = arr + S;
for(;arr < p; arr++) {
printf("%d ", *arr);
}
}
int main() {
int arr[S] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
print_arr(arr, S);
return 0;
}
if you try without a semicolon (;) then it gives the following error
main.c:7:23: error: expected ';' before ')' token
7 | for(arr < p; arr++) {
| ^
| ;

A for loop consists of four parts:
for(init;condition;iteration) { body }
For the compiler to know what is used as init, condition or iteration, you separate it via semicolon. if you want to keep one of the parts empty, you simply write a semicolon.
for(;i < 10;i++) is a loop that has no initialisation. only a condition and an iteration statement.
for(int i= 0;; i++) has no condition and
for(int i=0;i < 10;) has no iteration statement.
writing for(i < 10; i++) is simply not valid syntax.

Related

Not getting the correct output elements from an array in C [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I'm trying to write a code that inputs a number each time into an array and than print out the result from the arrays elements but for some reason i either get an infintie loop or it prints out the same number.
void main() {
char arr[SIZE];
int k = 1;
int i = 0;
while (k != 0) {
scanf("%d", &k);
arr[i] = k;
i++;
}
arr[i] = '\0';
int b = 0;
while (b < i) {
printf("elements are %d\n", arr[i]);
b++;
}
You want to print arr[b] and not arr[i].
Thus, you want: printf("elements are %d\n", arr[b]);
As you [currently] have it, printing arr[i] will always print the same element and it's UB because at that point i is one beyond the end of the arr array, so the value will be unknown/undefined.

Converting "c99" loop to regular stuff [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 3 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
Story: I tried to convert a c99 script to regular gcc.
Problem: The output is empty.
Expected output: 3,2,1
length is the number of elements in the array.
Update: the script is designed to sort the elements of the array in a descending order.
The code:
#include <stdio.h>
int main() {
int arr[] = { 1,2,3 };
int temp = 0;
int length = sizeof(arr) / sizeof(arr[0]);
int i = 0;
int j = i + 1;
for (i < length; i++;) {
for (j < length; j++;) {
if (arr[i] < arr[j]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
int y = 0;
for (y < length; y++;) {
printf("%d ", arr[y]);
}
return 0;
}
Your syntax for for loops is the issue.
Here is the correct way to write your loops.
int i, j;
for (i = 0; i < length; ++i) // for (initialisation; test condition; operation)
{
for (j = i + 1; j < length; ++j) // note that j is initialized with i + 1 on each iteration of
// the outer loop. That's what makes the bubble sort work.
{
/* test and swap if needed */
}
}
for (i = 0; i < length; ++i) // note that i is reset to zero, so we can scan the array from
// a known position (the top) to bottom.
{
/* printout */
}
Your semicolon is in the wrong place, move it to the far left just inside the parentheses.
Loop syntax is:
for (intializer; break condition; iterator)

Sorting an array - C language [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I have tried to write a sorting function sort(int *buffer, int array[], int size) which works in a way similar to the insertion sort - it takes the first element from the array, sets it as the first element of the buffer and then checks whether or not the next value showing up in the array is greater than the last value stored in the buffer. If yes, it keeps swapping the two elements until everything is in its place. This is my minimal working example:
#include <stdio.h>
void sort(int *buffer, int array[], int size) {
for(int i = 0; i < size; i++) {
buffer[i] = array[i];
while(i >= 1 && buffer[i] < buffer[i-1]) {
int tmp = buffer[i-1];
buffer[i-1] = buffer[i];
buffer[i] = tmp;
printf("i = %d i: %d, i -1 : %d \n",i, buffer[i], buffer[i-1]);
i--;
}
}
}
int main(void) {
int array[3] = {4,3,2};
int buffer[3];
sort(buffer, array, 3);
for(int i = 0; i < 3; i++) {
printf("%d", buffer[i]);
}
}
However, the output of this program is 222
To be honest, I don't see how it's even possible that three identical elements got placed in the buffer.
What can have gone wrong?
You are using the same variable for the inner while cycle and for the outer for loop. Use a different variable and copy the value of i to it in each iteration of the for.

Why are the wrong array values being printed? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
For some reason, the nested for loop I have created at the bottom seems to be printing out the wrong first value (Gives me 3, when it should be 8). Yet, when I simply do printf (at the bottom), I am given the right value. Not really sure what's wrong with my code.
#include <stdio.h>
int main(void)
{
int d;
printf("Please input dimensions: (between 3 and 9, inclusive): \n");
scanf("%i", &d);
int array[d][d];
int k = 1;
for (int i = 0; i < d; i++)
{
for (int j = 0; j < d; j++)
{
array[i][j] = (d * d) - k; //d^2 doesn't work to square a function
k++;
}
}
for (int z = 0; z < d; z++)
{
for (int y = 0; y < d; y++)
{
printf("%i\n", array[z][y]);
}
}
printf("%i\n", array[0][0]);
printf("%i\n", array[0][1]);
}
Edit: Sorry guys, the top value that was being printed was my own input. I was simply thinking it was the first value being printed.
Are you sure you aren't looking at your own input? I cut and paste and see:
Please input dimensions: (between 3 and 9, inclusive):
3
8
7
6
5
...
The 3 is actually what I typed and is echoed.

Finding the maximum value in a matrix (C programming) [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I'm learning to code in C. I've wrote the following code to find the maximum value inside a matrix, but for some reason the program would return the highest value in the first row (87), except the desired 99. I can't find the flaw in the code. Would be really happy for some help!
#include <stdio.h>
int Maxmin(int a[][4], int row, int col) {
int i, j, max;
max = a[0][0];
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
if (a[i][j] > max)
max = a[i][j];
}
return max;
}
}
void main() {
int a[3][4] = {
{ 3, 87, 11, 23 },
{ 99, 78, 19, 44 },
{ 59, 60, 13, 14 }
};
int num;
num = Maxmin(a, 3, 4);
printf("%d\n", num);
}
Not really easy to spot as usually bugs like this are down to i & j typos.
The return max; is inside the row's for loop.
Move it to the end of the function and then you'll examine every row of the matrix. I actually compiled and ran this fix.
The way to discover such errors, is to either put in extra print statements, like :
printf( "a[%d][%d]=%d ", i, j, a[i][j]);
Or to step through your program with a debugger, setting a break point on the code you're interested in.

Resources