Why is my program compiling but not doing anything? [closed] - c

Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 5 years ago.
Improve this question
So I'm working on a program and I'm initializing a 2D array with zeroes like so:
int studentGrades[3][2];
for(i = 0; i <= 3; i++){
for(j = 0; j <= 2; j++){
studentGrades[i][j] = 0;
}
}
printf(" %d", studentGrades[1][2]);
This compiles but does not do anything in the console, and will not advance past this point.
I've looked over it for a long time and I just can't crack it.
Thanks!
EDIT: Sorry, I forgot to include a line below the code to print out a position in the array, but the program doesn't get past the loop.

A problem in what you're showing is the <= in the loops. Since arrays are 0-based the last addressable location is n-1 where n is the size of your loops. So you are definitely going over the bounds of your array. That will cause a problem at some point. Also you aren't declaring i or j but that would have been caught by a compiler so you must be somewhere.
As for why you don't see any output nothing in your code above prints anything.
you need a loop to print the results. Something like this.
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 2; j++) {
print("Grade[%d][%d]: %d\n", i, j, studentGrades[i][j]);
}
}

... but does not do anything in the console...
What do you expect it to do in the console? You are never calling print statements or reading from stdin in the code snippet you have provided.
As a heads up, this is incorrect looping:
int studentGrades[3][2];
for(i = 0; i <= 3; i++){
for(j = 0; j <= 2; j++){
studentGrades[i][j] = 0;
}
}
Your array is 3x2, so you cannot access indices studentGrades[3] or studentGrade[AnyNumber][2]. Your accessible index ranges are 0-2 for rows and 0-1 for columns. As such, your loop should be:
int studentGrades[3][2];
for(i = 0; i < 3; i++){
for(j = 0; j < 2; j++){
studentGrades[i][j] = 0;
}
}
As for why you are not getting console I/O, I have no clue because the code snippet provided does not ever attempt to interface with the console.

I am assuming that you mean that you expect some kind of output from the code that you posted above. You don't have anything in your code such as a printf that writes output to the console. To do this you would have to include the preprocessor directive by doing #include <stdio.h> then using printf to print the contents of the array to the console.

Related

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)

Prime number into an array in C [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 6 years ago.
Improve this question
I try to do a function in order to sort an array and display after that only the prime number. But all the elements of my array are random numbers, and the problem is that the function display only negative prime numbers and not positive like 7 and 3, what can I do in order to solve the problem
int prime_arr(int size, int *arr, int *sort_arr)
{
int i, j, k = 0, flag;
for (i = 0; i < size; i++)
{
flag = 0;
for (j = 2; j < arr[i]/2; j++)
{
if (arr[i] % j == 0){
flag = 1;
break;
}
}
if (flag == 0){
sort_arr[k++] = arr[i];
}
}
return j;
}
I see 3 problems with the code:
1. You should return k, not j. k is the size of sort_arr
2. You should loop until arr[i] / 2, not one less than that (see <= in code below)
3. You do not handle negative numbers. Change your loop to the following:
for (j = 2; j <= abs(arr[i])/2; j++)
Without the code that prints your values, I'm not sure exactly what you're looking for, but hopefully fixing these will fix your problem.

Functions with Character Array? [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
I am really struggling with learning programming via C. I seem to get the gist of things while going through the book's exercises but the second I try to implement something different it falls apart especially in regard to arrays.
I am making a simple game with a 2D character array. It should be 11x11. I am trying to write a function to set each index of the array as a blank space ' ' to start. My code compiles then I get a 'core dump' when I run it. Here is the code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/*-------- GLOBAL FUNCTIONS --------*/
// Clear the Screen
void clear()
{
system("clear"); // For Linux/iOS
}
//---------- Creat Array(Game Board)
#define MAX 11 // Max number of characters in each row and column of array
char GameBoard[MAX][MAX];
// Clear Each Space in Array with Empty ("") Space
void ClearBoard(char GB[MAX][MAX])
{
for (int i = 0; i < MAX; ++i)
{
GameBoard[i][i] = ' ';
for (int j = 0; j < MAX; ++i)
{
GameBoard[i][j] = ' ';
}
}
}
int main()
{
ClearBoard(GameBoard);
return 0;
}
Any help towards understanding this better would be greatly appreciated, thank you.
your inner loop increments i until it is out of the array bounds
it should be ++j and not ++i
after MAX+1 iterations of the inner loop you're trying to access memory which is not allocated and that is why you get your error.
also the line GameBoard[i][i] = ' '; is unnecessary since it is taken care of in the inner loop when j == i
You need to change this line
for (int j = 0; j < MAX; ++i)
to
for (int j = 0; j < MAX; ++j)
Replace ++i with ++j in the line
for (int j = 0; j < MAX; ++i)
GameBoard[i][i] = ' ';
This line doesn't make any sense. Remove it.
for (int j = 0; j < MAX; ++i)
Should be j++.

error while using array in C [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 7 years ago.
Improve this question
int main() //task 10
{
int num[9], i, counter = 0, minNum, maxNum = 0, sum = 0;
for (i = 0; i <= 9; ++i)
{
scanf("%d", &num[i]);
if (num[i] > maxNum)
{
maxNum = num[i];
minNum = maxNum;
}
else if (num[i] < minNum)
minNum = num[i];
sum += num[i];
}
printf("minNum: %d, maxNum: %d\nThe average is:%d\n", minNum, maxNum, sum / 10);
return 0;
}
While trying to run this program i get this error:
Run-Time Check Failure #2 - Stack around the variable 'num' was corrupted.
I would like to know what is wrong with my array.
Using code below you will read the array out of bounds
for (i = 0; i <= 9; ++i)
you can only access elements from 0 up to and including 8 in this case. Change to i < 9.
PS. There maybe other flaws in the logic (check some comments). This one here is also tricky
if (num[i] < minNum)
minNum is not initialized, so if above condition is evaluated in loop first, you will get undefined behaviour due to reading uninitialized variable.
Change <=9 in the for loop at the numbering starts at zero. Use <9 instead

Does (int i = 0; i < n; i++;) used as a block instead of {int i = 0; i < n; i++;} make any sense at all in C?

Don't know if it's a really dumb thing to ask as I feel it goes against C syntax.But I am not sure.I stumbled across that in a question posted few minutes back.The OP uses something like (int i = 0; i < n; i++), ie without even a ; after i++.
Fibonacci Series in C - The series of numbers up to a given number
But though the OP's line is obviously wrong, I am tempted to ask something I just don't know- What does the following mean in C :
(int i = 0; i < n; i++;) // Three `;` terminated statements enclosed in ()
as the following simply means a block of statements in C:
{int i = 0; i < n; i++;}
I mean, what does (int i = 0,n=3; i = n; i++;) mean in the following dummy program:
#include<stdio.h>
int main(void)
{
(int i = 0,n=3; i = n; i++;)
}
Edit Even that single line sourced from that original question is ridden with errors.So let me ask this independently : What does it do if we enclose multiple ; terminated statements within a pair of ()? If we enclose within {} it becomes a block,but what about ()?
Nothing. The parentheses are used in certain situations such as boolean expressions and for loop comprehensions. You'll get a bunch of syntax errors.
Common for loop construction:
for (int i = 0; i < 10; i++){
//code here
}
The code
{int i = 0; i < 10; i++;}
doesn't really do much except set i to 0 and increment it to 1.
I'm not even sure if saying i < 10 is valid outside a condition

Resources