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 last month.
Improve this question
I am a beginner in the programming language of C, coming from mainly Python/MATLAB. I wanted to try and figure out what the problem with the following section of code is:
#include <stdio.h>
#include <math.h>
int PSR(int n) {
int psr = 1;
// psr_up = int(sqrt(double psr)));
for (int i = 1; i < 200; i++) {
if (n % i == 0) {
psr = i;
}
}
return psr;
}
At the moment, I keep getting the following error when I attempt to compile:
Undefined symbols for architecture arm64:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I was looking to see if anybody could point me in the right direction, as this seems to be basic syntax, which I believe I am following the CodeAcademy suggestions for a for loop precisely.
EDIT:
Apparently, you need a main() function for it to compile.
// Project Euler Problem 266
// https://projecteuler.net/problem=266
#include <stdio.h>
#include <math.h>
int PSR(int n) {
int psr = 1;
// psr_up = int(sqrt(double psr)));
for (int i = 1; i < 200; i++) {
if (n % i == 0) {
psr = i;
}
}
return psr;
}
int main() {
printf("%d", PSR(3102));
}
The problem is now it outputs 141% whereas I expected 47.
First you need to return from PSR once you find the first divisor. Second you need to add a new line.
#include <stdio.h>
#include <math.h>
int PSR(int n) {
int psr = 1;
// psr_up = int(sqrt(double psr)));
for (int i = 1; i < 200; i++) {
if (n % i == 0) {
psr = i;
break; // for exit of loop
// or simply 'return i;'
}
}
return psr;
}
int main() {
printf("%d\n", PSR(3102)); // new line added after print
}
Related
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 months ago.
Improve this question
I'm working on a problem set for CS50 in C but I keep getting an error readability.c:11:1: error: expected identifier or '(' { and I'm not sure what's wrong. Any help would be much appreciated.
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int count_letters(string text);
int main(void);
{
string paragraph = get_string("Input text: ");
count_letters(paragraph);
int L = 0;
int S = 0;
int index = 0.0588 * L - 0.296 * S - 15.8;
printf("%i", index);
}
int count_letters(string text)
{
int letters = 0;
for (int i = 0, length = strlen(text); i < length; i++)
if isalpha(text[i]) {
letters + 1
}
return letters;
}
You have a semicolon at
int main(void);
and you need one here:
for (int i = 0, length = strlen(text); i < length; i++){ //added {
if(!(isalpha(text[i]) == 0)){ // is alpha in brackets too and I usually prefer to check result with proper types
letters + 1; //addded ;
}
}
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 1 year ago.
Improve this question
(the problem is caused by me and has been solved ,greetings from the newbie)I apologize to everyone, my function type was integer i just realized it, I opened it because I worked for hours, I have to delete it).
I am using gcc 9.3.0 version and standard settings. In the code in the example, I am getting 7 while waiting to get output 7.14. What could be the reason for this?(im using gcc main.c -lm -o main when compiling,what I'm trying to do is print as an integer if the double number is equal to integer, otherwise print it as a double,sorry for the adjustments, this is the last and proper version)
#include <stdio.h>
#include <math.h>
int try(double a);
int main() {
double b = 7.14;
double x;
double temp;
x = try(b);
if (fmod(x, 1) == 0.0) { // print 7 in this function
temp = x;
printf("%d", (int)temp);
}
else if (fmod(x, 1) != 0.0) {
printf("%f", x);
}
return 0;
}
int try(double a) {
if (fmod(a, 1) != 0.0) {
printf("%lf", a); // print 7.14 in this function
} else if (fmod(a, 1) == 0.0) {
printf("%d", (int)a);
}
return a + 0;
}
With gcc version 10.2.0 (GCC)
Got the result 7.140000
Some code-format and debug output.
#include <math.h>
#include <stdio.h>
double func_try(double a) { return a + 0; }
int main() {
double b = 7.14;
double x;
x = func_try(b);
printf("%lf\n", fmod(x, 1));
if (fmod(x, 1) != 0.0) {
printf("%lf", x);
} else if (fmod(x, 1) == 0.0) {
printf("%d", (int)x);
}
return 0;
}
$gcc main.c -lm -o main
$./main
0.140000
7.140000
fmod(7.14, 1) = 0.14, thus it hit the first if and print 7.14.
Compute remainder of division
Returns the floating-point remainder of numer/denom (rounded towards zero):
fmod = numer - tquot * denom
Where tquot is the truncated (i.e., rounded towards zero) result of: numer/denom.
http://www.cplusplus.com/reference/cmath/fmod/
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 want to create a program who store the rest of successive division in the array.But unfortunately the array i created refuse store the remain correctly for example 5/2 the remain is suppose to be 1 but the array store another value
#include <stdio.h>
#include <stdlib.h>
int divise(int n){
int i=0;
int remain[20];
int rest;
while(n!=0){
rest = n%2;
remain[i] = rest;
n = n/2;
i++;
printf("%d\n",remain[i]);
}
}
int main(){
divise(10);
}
The mistake is with your i++ statement . It should be after printf("%d\n",remain[i]);.
Modified code :-
#include <stdio.h>
#include <stdlib.h>
int divise(int n)
{
int i = 0;
int remain[20];
int rest;
while (n != 0)
{
rest = n % 2;
remain[i] = rest;
n = n / 2;
printf("%d\n", remain[i]);
i++; // repositioned
}
}
int main()
{
divise(10);
return 0;
}
Output :-
0
1
0
1
Your function int divise(int n) do not return any int values . So better make it void divise(int n) .Also int main() should have a return 0 .
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
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.
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.
Improve this question
In this factorial recursion "printf" outputs 2 6 8, Can't understand why...
#include <stdio.h>
int f(int n)
{
int k;
if (n==1)
return 1;
else
k = n*f(n-1);
printf("%d ",k);
}
int main()
{
f(4);
}
The original version of the code in the question was:
int f(int n) {
int k;
if (n == 1)
return 1;
else
k = n * f(n - 1);
printf("%d ", k);
}
int main() {
f(4);
}
This code has undefined behavior because you do not return a value properly if n != 1, causing the calling code to use an unpredictable value in its own calculation. The behavior is undefined, anything can happen.
Adding the return statement fixes this issue. Note however these extra points:
variable k is useless in the f function.
f should return 1 for an argument of 0.
you should output a newline after the number and return 0 in main.
Here is modified version:
#include <stdio.h>
int f(int n) {
if (n <= 1)
return 1;
else
return n * f(n - 1);
}
int main() {
int i;
for (i = 0; i < 10; i++) {
printf("%d! = %d\n", i, f(i));
}
return 0;
}
There are many mistakes you are making here:
printf("%d ",k);
this line is never going to be executed because in any case if() else clause will return before it.
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'm receiving Output: 1. I should count the number of times a digit appear in an integer, for example, for number 1222345 and n = 2 Should appear 3 times.
int countOccurrences(int n, int num)
{
int i,k;
i=0;
while(num!=0)
{
k=num%10;
num=num/10;
if(k==n)
{
i++;
}
}
}
// Main
void main()
{
int num= 1222345;
int n = 2;
printf("Occurance of a number: %d", countOccurrences(n,num));
}
You have undefined behavior in the code. The function is supposed to return an int and it didn't.
Solution is to add return i in the end of other function. This will give you correct result. In the countOccurrences() function
...
if(k==n)
{
i++;
}
}
return i;
}
I was skipping the discussion of error check and all that. As chux mentioned for n<=0 case you might want to add a different way of handling it but you didn't add it. Atleast consider those case and put an error message on whatever input you need.
Some corner cases are
n=0,m=0.
Negative value of n or m.
Put a return on your countOccurrences function please
int countOccurrences (int n, int num) {
int i, k;
i = 0;
while (num! = 0)
{
k = num% 10;
num = num / 10;
if (k == n)
{
i ++;
}
}
return i; }
As other have pointed out, there are important issues with your code.
Here is a recursive solution that you may find interesting:
int countOccurrences(int n, int num)
{
int count = ((num % 10) == n);
return (num < 10) ? count : count + countOccurrences(n, num / 10);
}
Few general remarks about your code:
When using printf(), you should #include <stdio.h>.
main() should return int.
Place spaces around operators and format your code consistently. This k = num % 10; is more readable than k=num%10;. (There's more to code formatting than a matter of taste; without spaces you create areas full of characters which are more difficult to parse for our visual system.)