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.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year.
Improve this question
{
for (int i = 0; i <= 5; i++)
{
for (int j = 5; j >= i; j--)
{
printf("+");
}
printf("\n");
}
}
At i = 4; j will be 2 how does it satisfy the condition of j>=i?
You seem to be thinking that "j will be 2" because you see two "+" in output in that line.
But that only means that the inner loop is executed twice. Which is because (quoting Barmars comment):
When i == 4, the inner loop will only loop twice, with j == 5 and j == 4.
You analysed the detailed behaviour of your code based on output, which is good. But if something puzzles you then you either need more output on exactly the detail which puzzles you; e.g. by actually outputting the value in question for verifying your assumptions. Or you could use a debugger.
For example. I changed your code in a way which probably makes things very obvious:
#include <stdio.h>
int main(void)
{
for (int i = 0; i <= 5; i++)
{
printf("%d: ", i);
for (int j = 5; j >= i; j--)
{
printf("%d", j);
}
printf("\n");
}
}
It gets you an output (e.g. here https://www.onlinegdb.com/online_c_compiler ) of:
0: 543210
1: 54321
2: 5432
3: 543
4: 54
5: 5
Where the line 4: 54 indicates two inner loop iterations, with values for j of 5 and 4, while i is 4.
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)
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.
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
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
Assume the character a,b,c,d,e represent the number 1 to 9, and they cannot be equal to
each other.
Question:
How many equals that can meet (ab * cde = adb * ce).
Example:
36 * 495 = 396 * 45.
Here is my code,and the result is right.However,i think my code is too awkward,especially in (if(a!=b&&a!=c&&a!=d&&a!=e&&b!=c&&b!=d&&b!=e&&c!=d&&c!=e&&d!=e&&c*d*e!=0))
I would appreciate it if someone could give me a better solution.
#include<stdio.h>
main(){
int a,b,c,d,e,m,n,i=0;
long f1,f2,f3,f4;
for(m=11;m<=99;m++){
a=m/10;
b=m%10;
if(a!=b&&a*b!=0)
{
for(n=101;n<=999;n++)
{
c=n/100;
d=n%100/10;
e=n%10;
if(a!=b&&a!=c&&a!=d&&a!=e&&b!=c&&b!=d&&b!=e&&c!=d&&c!=e&&d!=e&&c*d*e!=0)
{
f1=a*10+b;
f2=c*100+d*10+e;
f3=a*100+d*10+b;
f4=c*10+e;
if(f1*f2==f3*f4) i++;
printf("\n%d%d*%d%d%d*=%d%d%d*%d%d\n",a,b,c,d,e,a,d,b,c,e);
}
}
}
}
printf("%d\n",i);
return 0;
}
If you can, instead of
int a,b,c,d,e;
Try to use
int numbers[5];
And then to check if your numbers are all different, you can use for loops
doubleOccurence = FALSE; /* where FALSE = 0 */
for (i=0; i < 4; i++) {
for (j=i+1; j < 5; j++) {
doubleOccurence = doubleOccurence || (numbers[i] == numbers[j]);
}
}
It looks a bit clearer to me.
Unfortunately you can't really iterate through a list of variables you are better off with an array of numbers like Julien mentions in his answer.
int nums[5];
replace a with nums[0], b with nums[1], etc....
But then I would go one step further to tidying up your code and call a function that takes in the array to check uniqueness:
if(listIsUnique(nums, 5)) // yes hardcoded the 5, but that can be sorted
{
...
}
And then:
bool listIsUnique(int* nums, int len)
{
for (int i = 0; i < len; i++)
for (int j = i + 1; j < len; j++)
if (nums[i] == nums[j])
return false; // return false as soon as you find a match - slightly faster :)
return true; // if we get here its a unique list :)
}
Note: code is untested, there may be mistakes :o