Simple for-loop countdown timer in C [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 7 years ago.
Improve this question
This is what the code should do:
“Lift off in T minus 5 4 3 2 1 Blast-off!”
When I run it, it just keeps printing ''Sum = 5'' forever.
Code:
int main(void) {
int sum = 5;
int i;
printf("Lift off in T minus\n");
for (i = 0; i < 5; i=i+i) {
sum = sum - i;
printf("sum = %d\n",sum);
}
printf("Blast-off",sum);
return 0;

for (i = 0; i < 5; i=i+i) { // use i = i+1
sum = sum - i; //sum-- or sum = sum -1
printf("sum = %d\n",sum);
}
As initially i=0, so
i=i+i; //will be zero always, no increment.
And
sum = sum -1;
otherwise
i = 0 =>sum = sum - i; // = 5 as i=0
i = 1 =>sum = sum - i; // = 4 as i=1
i = 2 =>sum = sum - i; // = 2 as i=2
i = 3 =>sum = sum - i; // = -1 as i=3

Why not run the loop backwards?
for (i = 5; i > 0; --i) {
printf("i = %d\n",i);
}
This is simpler so the potential for bugs to creep in is reduced. Also, your final printf if malformed: you're missing a format specifier for sum.
Your specific problem: replace i=i+i with i=i+1 or something similar. (I prefer ++i).

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)

Different declarations of matrix 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 4 years ago.
Improve this question
I made the following's declaration:
Implementation 1:
int m[3][3];
m[0][0] = 1;
m[0][1] = 4;
m[0][2] = 5;
m[1][0] = 5;
m[1][1] = 7;
m[1][2] = 6;
m[2][0] = 5;
m[2][1] = 8;
m[2][2] = 8;
Implementation 2:
int m[3][3];
int m = {
{1,4,5},
{5,7,8},
{5,8,8},
};
Implementation 1 works just fine, but Implementation 2 results in a wrong output in my code (which is too long to put here) and I wanted to use 2 because is more compact. Why is the result in my output being different?
For me, the following code works perfectly:
#include<stdio.h>
int main()
{
int m[3][3] = {
{1,4,5},
{5,7,8},
{5,8,8},
};
for(int i = 0; i<3; i++){
for(int j = 0; j<3; j++){
printf("%d \n",m[i][j]);
}
}
return 0;
}
Output:
1
4
5
5
7
8
5
8
8

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.

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.

Distinguishing between two array indices? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm having trouble with arrays. I copied this code from a book:
#include <stdio.h>
#include <stdbool.h>
int main (void)
{
int p, i, primes[50], primeIndex = 2;
bool isPrime;
primes[0] = 2;
primes[1] = 3;
for (p = 5; p <= 50; p = p + 2) {
isPrime = true;
for (i = 1; isPrime && p / primes[i] >= primes[i]; ++i)
if (p % primes[i] == 0)
isPrime = false;
if (isPrime == true) {
primes[primeIndex] = p;
++primeIndex;
}
}
for (i = 0; i < primeIndex; ++i)
printf ("%i ", primes[i]);
printf ("\n");
return 0;
}
In particular, I'm having trouble understanding the difference between the primeIndex and the i variables. The primeIndex refers to the array number and i refers to the number placed into the array. Right?
primeIndex is the place where the next found prime is written in the prime array, and also the number of primes known so far. i is the index of the prime used for trial division. For each candidate, i loops from 1 (we don't need to try out primes[0] = 2 because only odd numbers are checked) to the index of the first prime larger than the square root of the candidate.

Resources