For some strange reason every time I compile my project I get this strange error of multiple definitions even though this is the only place I define my function. I use code blocks as my IDE and GCC as the compiler. Here is the code:
#include <stdio.h>
#include <test.h>
int checkForSquare(int num){
int squared[10001];
squared[num] = num * num;
for(int i = 0; i <= 10000; i++){
if(squared[i] == num){
return 1;
break;
}
}
return 0;
}
int main(){
for(int x = 0;x <=10000;x++){
if(checkForSquare(x))
printf( "%d" , x );
else
printf("Not Square");
}
return 0;
}
First, you can check the discussion below:
How to prevent multiple definitions in C?
Second, please properly align your code at least before you post your question.
Third, I think your checkForSquare() should look like this:
int checkForSquare(int num){
static long squared[10001];
squared[num] = num * num;
for(int i = 1; i < num; ++i){
if(squared[i] == num){
return 1;
}
}
return 0;
}
Related
I have to convert a letter to binary number. All work but with one problem - I don`t understand why after my binary number it still prints some other numbers... Can someone help, please?
Here is my code. Thank you in advance!
#include <stdio.h>
#include <stdbool.h>
void convert(const char char, bool bits[8]) {
char c = char;
for (int j = 7; j+1 > 0; j--){
if(c>=(1<<j)){
c=c-(1<<j);
printf("1");
}else{
printf("0");
}
}
//here start to prints other numbers
printf("\n");
printf("\n");
}
int main(){
bool bits1[8];
encode_char('A', bits1);
for(int i = 0; i < 8; i++)
{
printf("%d", bits1[i]);
}
printf("\n");
return0;
}
There are 3 problems making your sample code unable to compile:
You tried to declare the first argument to your function as const char char, but char is a type and is not valid variable name.
You tried to call encode_char in main, but you defined convert
return0 should be return 0
After fixing those, there will still be a problem with garbage values.
Because even though you passed bits, the function never does anything with it (so it remains with garbage values).
So your printf("%d", bits1[i]); will be just a bunch of "random" numbers. The extra numbers are not from what you marked with //here....
Here is an example that assigns useful values to bits:
#include <stdio.h>
#include <stdbool.h>
void encode_char(const char input_char, bool bits[8]) {
char c = input_char;
for (int j = 7; j >= 0; j--){
if(c>=(1<<j)){
c=c-(1<<j);
bits[7-j] = 1;
}else{
bits[7-j] = 0;
}
}
}
int main(){
bool bits1[8];
encode_char('A', bits1);
for(int i = 0; i < 8; i++)
{
printf("%d", bits1[i]);
}
printf("\n");
return 0;
}
For a school assignment, I have been tasked with making a simple codebreaker game using the functions and prototypes provided. You should be able to input the length and difficulty of the code, and type in numbers to make your guess. The part I'm having trouble with is determining the Perfect and Imperfect matches. The program needs to tell the player how many perfect matches (correct number in the correct place) their guess had, and then reveal the perfect matches. The latter I have correct, but the former is only returning with 56 perfect matches when I test it. And I don't really know where to start with the Imperfect matches section, and how I can get it to skip perfect matches. Is there any way I can do this without strings?
Keep in mind, this is a section of the code, not the entire program. It's in this section that things are breaking.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void GetGuess(int guess[], int length) {
int i;
printf("\n\nPlease input your guesses.\n");
for(i = 0; i < length; i++){
scanf("%d", &guess[i]);
}
return;
}
void ProcessGuess(int hidelist[], int showlist[], int length, int guess[]) {
int i;
int perfectsum, imperfectsum;
for(i = 0; i < length; i++){
if(guess[i] == hidelist[i]){
PerfectMatches(hidelist, guess, length);
showlist[i] = guess[i];
}
else{
ImperfectMatches(hidelist, guess, length);
}
}
printf("You have %d perfect matches and %d imperfect matches!\n\n", perfectsum, imperfectsum);
return;
}
int PerfectMatches(int hidelist[], int guess[], int length) {
int i;
int perfectsum = 0;
for(i = 0; i < length; i++){
if(guess[i] == hidelist[i]){
perfectsum++;
}
}
return perfectsum;
}
int ImperfectMatches(int hidelist[], int guess[], int length) {
int i, j;
int imperfectsum = 0;
for(i = 0; i < length; i++){
for(j = 0; j < length; j++){
if(i != j){
if(hidelist[i] == guess[j]){
imperfectsum++;
break;
}
}
}
}
return imperfectsum;
}
void copyArray(int dest[], int source[], int length) {
}
Firstly in your function ProcessGuess you didn't get the return of the PerfectMatches and ImperfectMatches functions. So your perfectsum and imperfectsum aren't correct.
Then concerning the Imperfect Matches section you can use the PerfectMatches function but using != instead of == (if I understood the rules correctly)
In the below code, I am saving and printing two vectors. This means I have created each function —scanf() and printf()— twice even though they are the same apart from the vector name they operate. How could I have only one scanf() and printf() functions, and still save and print as many vectors as I want? N.b. In this case, I am only working with static vectors.
#include <stdio.h>
int scanning_first_vector(int *vector1);
int printing_first_vector(int *vector1);
int scanning_first_vector(int *vector2);
int printing_first_vector(int *vector2);
int main()
{
int vector1[5], vector2[5];
printf("Please enter the first vector.\n");
scanning_first_vector(vector1);
printing_first_vector(vector1);
printf("\nPlease enter the second vector.\n");
scanning_first_vector(vector2);
printing_first_vector(vector2);
return 0;
}
int scanning_first_vector(int *vector1)
{
int i;
for (i = 0; i < 5; ++i)
{
scanf("%d", &vector1[i]);
}
return 0;
}
int printing_first_vector(int *vector1)
{
int i;
for (i = 0; i < 5; ++i)
{
printf(" %d ", vector1[i]);
}
return 0;
}
int scanning_second_vector(int *vector2)
{
int i;
for (i = 0; i < 5; ++i)
{
scanf("%d", &vector2[i]);
}
return 0;
}
int printing_second_vector(int *vector2)
{
int i;
for (i = 0; i < 5; ++i)
{
printf(" %d \n", vector2[i]);
}
return 0;
}
I think I just did below the leaner version of the code after reading the comments — thank you, guys! :-) I understand now that I can use the same function & I only need to make sure I give distinctive names to the vectors in the main() function. It works well, but it would also be awesome to get confirmation that the way the code is done here is as lean & good as it can get :-) Thank you!
#include <stdio.h>
int scanning_vector(int *vector);
int printing_vector(int *vector);
int main()
{
int vector1[5], vector2[5];
printf("Please enter the first vector:\n");
scanning_vector(vector1);
printing_vector(vector1);
printf("\nPlease enter the second vector:\n");
scanning_vector(vector2);
printing_vector(vector2);
return 0;
}
int scanning_vector(int *vector)
{
int i;
for (i = 0; i < 5; ++i)
{
scanf("%d", &vector[i]);
}
return 0;
}
int printing_vector(int *vector)
{
int i;
for (i = 0; i < 5; ++i)
{
printf(" %d ", vector[i]);
}
return 0;
}
I tried this code
/*main.c*/
#include <stdio.h> /* printf */
#include <math.h> /* sqrt */
int frequency_of_primes (int n) {
int i, j;
int freq = n - 1;
for (i = 2; i <= n; ++i)
for (j = sqrt(i); j > 1; --j)
if (i%j==0) {--freq; break;}
return freq;
}
int main() {
printf("%f\n", sqrt(4.0));
return 0;
}
and compiled it with gcc main.c, it reported that undefined reference tosqrt'. I already know add-lm` option can resolve this issue. But what really surprises me is this:
#include <stdio.h> /* printf */
#include <math.h> /* sqrt */
// int frequency_of_primes (int n) {
// int i, j;
// int freq = n - 1;
// for (i = 2; i <= n; ++i)
// for (j = sqrt(i); j > 1; --j)
// if (i%j==0) {--freq; break;}
// return freq;
// }
int main() {
printf("%f\n", sqrt(4.0));
return 0;
}
The main function also calls sqrt, but ld doesn't report any errors.
That's because the optimizer is handling the constant case you're using.
It's the sqrt(i) call inside frequency_of_primes() that's the problem, the call in main() is optimized out. You can figure that out by reading the generated code for the latter case, it'll just load a constant 2.0 and be done with it.
I updated my main and sequetialSearch and now it crashes when it runs. It compiles okay, but then crashes.
main.c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
#include "percentage.h"
#include "sequentialSearch.h"
#define searchAmount 100
int main(int argc, char *argv[])
{
int numbers[100];
int searches[searchAmount];
int testAmounts[searchAmount];
int i;
int where;
int searchSuccess;
int searchUnsuccess;
int percent;
int looker;
int sum;
int average;
srand(time(NULL));
for (i = 0; i < 100; i++){
numbers[i] = rand() % 200;
}
for (i = 0; i < searchAmount; i++){
searches[i] = rand() % 200;
}
searchUnsuccess = 0;
searchSuccess = 0;
sum = 0;
for(i = 0; i < searchAmount; i++){
if(seqSearch(numbers, 100, searches[i], &where, &looker)){
searchSuccess++;
testAmounts[i] = looker;
}else{
searchUnsuccess++;
testAmounts[i] = looker;
}
}
for(i = 0; i < searchAmount; i++){
sum = sum + testAmounts[i];
}
average = sum / searchAmount;
percent = percentRate(searchSuccess, searchAmount);
printf("Total number of searches: %d\n", searchAmount);
printf("Total successful searches: %d\n", searchSuccess);
printf("Success Rate: %d%%\n", percent);
printf("Total number of tests ran: %d\n", average);
system("PAUSE");
return 0;
}
sequentialSearch.h
bool seqSearch (int list[], int last, int target, int* locn, int* looker){
*looker = 0;
while(*looker < last && target != list[*looker]){
*looker++;
}
*locn = *looker;
return(target == list[*looker]);
}
Pass looker in by reference, so that you can access its value from the caller.
int looker;
...
for(i = 0; i < searchAmount; i++){
if(seqSearch(numbers, 100, searches[i], &where, &looker)){
searches[i] += looker;
searchSuccess++;
}else{
searchUnsuccess++;
}
}
bool seqSearch (int list[], int last, int target, int* locn, int *looker){
*looker = 0;
while(*looker < last && target != list[*looker]){
(*looker)++;
}
*locn = *looker;
return(target == list[*looker]);
}
By the way, you may wish to reconsider defining functions in your header file; this could cause problems with duplicate symbol when linking if you have more than one c file including this file.
Why not just pass looker in as an int*, use it essentially as you have been, look at the value after seqSearch(...) returns, and add it to a running total back in main()?
One problem is that the increment of looker in seqSearch is incrementing the pointer rather than the value. It should probably be:
(*looker)++;