I'm working on an exercise problem for a programming class and I'm a bit out of my depth debugging it. My main problem is that I have had to convert strings into integers and vice-versa and I don't understand well how that works. I'll attach a copy of the exercise and another of my code. Any and all help is appreciated.
CODE:
#include <cs50.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
string Company_Check(long number);
bool Luhn (long number);
int main(void)
{
//Prompt for card Number
long card = get_long("Number:");
//Applying Luhn's Algorithm
bool validity = Luhn(card);
if (validity==true) //The card passed the test
{
//Finding and printing company name
printf("\nCompany: %s\n", Company_Check(card));
} else //The card didn't pass the test
{
printf("INVALID (Luhn’s Algorith)\n");
}
}
string Company_Check(long number)
{
//Declaring variables
char num[30];
char char_first_two[3]; //This is where I'll save the first two digits of the number
int int_first_two;
char char_first_one[3]; //This is where I'll save the first one digit of the number
int int_first_one;
//Creating the two 'First digits' var
int first_two = 10*(atoi(num[0]))+ atoi([num[1]);
int fist_one = atoi(num[0]);
//Analizing cases
if (first_two == 34 || first_two == 37)
{
printf("American Express\n");
} else if (first_two == 51 || first_two == 52 || first_two == 53 || first_two == 54 || first_two == 55)
{
printf("Mastercard\n");
} else{
//Could be (1) Visa or (2) INVALID
if (first_one == 4)
{
printf("Visa\n");
} else
{
printf("INVALID\n");
}
}
}
bool Luhn (long number) //Done
{
int i; //Used in the for loops
int sum1; //Used to tally the sums in Step 4
int sum2; //Used to tally the sums in Step 4
int total; //Used to tally the sums in Step 4
int cod; //Counter of Digits
int lastnumber; //Used in Steps 5 and 6
char num[30]; //Card number & total in String format
char fot[30]; //First Every-Other number list
char sot[30]; //Second Every-Other number list
//1. Converting the long NUMBER to a string NUM
sprintf(num, "%d", number);
//2. Calculating the number of digits of the number
cod = 0;
for(i=0; num[i+1]!= '\0' ; i++)
{
cod++;
}
//3. Creating two CHAR arrays of the first and second every-other number list
if (cod%2==0)//number of digits is even
{
for(i=0; num[i+1]!= '\0' ; i++)
{
if (i%2==0) //We're dealing with the FIRST set of every other numbers
{
fot[i/2]=num[i];
}
else //We're dealing with the SECOND set of every other numbers
{
sot[((int)(i/2))+1]=num[i];
}
}
fot[(cod/2)+1]= '\0';
sot[(cod/2)+1]= '\0';
} else //number of digits is odd
{
for(i=0; num[i+1]!= '\0' ; i++)
{
if (i%2==0) //We're dealing with the SECOND set of every other numbers
{
sot[i/2]=num[i];
}
else //We're dealing with the FIRST set of every other numbers
{
fot[((int)(i/2))+1]=num[i];
}
}
sot[(cod/2)+1]= '\0';
fot[(cod/2)+1]= '\0';
}
//4. Sum Algorithm
sum1=0;
sum2=0;
total=0;
for(i=0 ; fot[i+1]!= '\0' ; i++)
{
sum1 =+ atoi(fot[i]);
}
for(i=0 ; sot[i+1]!= '\0' ; i++)
{
sum2 =+ atoi(sot[i]);
}
total = sum1 + sum2;
//5. Isolating the last number
//converting the total into a string (using var NUM because its no longer needed)
sprintf(num, "%d", total);
//Calculating the number of digits
cod = 0;
for(i=0; num[i+1]!= '\0' ; i++)
{
cod++;
}
//putting the last number in a var
lastnumber = atoi(num[cod-1]);
//6. Studying cases
if (lastnumber==0)
{
return true;
}
else
{
return false;
}
}
Problem:
Problem
Related
I am currently trying to finish a code where a user inputs two 5 digit long numbers. The code then checks to see if there are any identical numbers in the same spot for the two numbers and displays how many identical numbers there are in the same spot of the two inputs. (ex. comparing 56789 and 94712 there would be one similar digit, the 7 in the 3rd digit place.) As of now I have been able to break down the inputs into the digits in each spot, I just need help comparing them. Originally I thought I could just create an int that would serve as a counter and use modulus or division to output a 1 whenever the digits were the same, but I have been unable to put together a formula that outputs a 1 or 0 depending on if the digits are alike or not.
suppose you know the length of strings n (as a condition you would need them to be equal, if they differ in length other validation is needed)
//n is the length of string
for(int i=0;i<n;i++)
{
if(string1[i]==string2[i])
{
//do something, make a counter that increments here...
//also save index i, so you can tell the position when a match occured
}else
{
//do something else if you need to do something when chars didnt match
}
}
Here you when i=0, you are comparing string1[0] with string2[0], when i=1, you compare string1[1] with string2[1] and so on.....
I'd recommend reading the two in as strings or converting to strings if you have the ability to. From there it's a simple string compare with a counter. Something like this:
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int is_numeric(char *str)
{
while (*str)
if (!isdigit(*str++))
return (0);
return (1);
}
int main(void)
{
char num1[32];
char num2[32];
int count = 0;
printf("Digit 1\n>> ");
if (scanf("%5s", num1) != 1 || !is_numeric(num1))
return (0);
printf("Digit 2\n>> ");
if (scanf("%5s", num2) != 1 || !is_numeric(num2))
return (0);
if (strlen(num1) != 5 || strlen(num2) != 5)
return (0);
for (int i=0; i<5; ++i)
if (num1[i] == num2[i])
++count;
printf("%d\n", count);
return (0);
}
You can do it very easy using modulo (%) and divide (/). First you do % 10 to get the least significant digit and do the compare. Then you do / 10 to remove the least significant digit. Like:
#include <stdio.h>
#include <string.h>
int main(void) {
unsigned int i1, i2;
int i;
int cnt = 0;
printf("Input first 5 digit number:\n");
if (scanf(" %u", &i1) != 1 || i1 < 10000 || i1 > 99999) // Get integer input and check the range
{
printf("input error\n");
return 0;
}
printf("Input second 5 digit number:\n");
if (scanf(" %u", &i2) != 1 || i2 < 10000 || i2 > 99999) // Get integer input and check the range
{
printf("input error\n");
return 0;
}
for (i=0; i<5; ++i)
{
if ((i1 % 10) == (i2 % 10)) ++cnt; // Compare the digits
i1 = i1 / 10;
i2 = i2 / 10;
}
printf("Matching digits %d\n", cnt); // Print the result
return 0;
}
It can also be done using strings. Read the input as unsigned int and then convert the value to a string using snprintf and finally compare the two strings character by character.
Something like:
#include <stdio.h>
#include <string.h>
int main(void) {
char str1[32];
char str2[32];
unsigned int i1, i2;
int i;
int cnt = 0;
printf("Input first 5 digit number:\n");
if (scanf(" %u", &i1) != 1) // Get integer input
{
printf("input error\n");
return 0;
}
snprintf(str1, 32, "%u", i1);
if (strlen(str1) != 5) // Convert to string
{
printf("input error - not 5 digits\n");
return 0;
}
printf("Input second 5 digit number:\n");
if (scanf(" %u", &i2) != 1) // Get integer input
{
printf("input error\n");
return 0;
}
snprintf(str2, 32, "%u", i2); // Convert to string
if (strlen(str2) != 5)
{
printf("input error - not 5 digits\n");
return 0;
}
for (i=0; i<5; ++i)
{
if (str1[i] == str2[i]) ++cnt; // Compare the characters
}
printf("Matching digits %d\n", cnt); // Print the result
return 0;
}
The reason for taking the input into a unsigned int instead of directly to a string is that by doing that I don't have to check that the string are actually valid numbers (e.g. the user type 12W34). scanf did that for me.
I had this working a second ago but accidentally broke it. Can anyone help me fix it? I'm getting a Segmentation Fault so I assume I messed up the pointers at some point. It's supposed to generate a bunch of random numbers depending on user input.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
unsigned int mySeed; // creates our variables
unsigned int taps[2];
unsigned int temp[2];
unsigned int myToggle;
FILE *fp;//opens the file
fp = fopen("random.txt", "w");//sets the file to open equal to this file
int TapInputs = 1;
int count = 0;
int tap;
int myNewNumber = 0;
mySeed = atoi(argv[1]);
if(atoi(argv[1]) > 0) //Error checking for negative inputs.
{
printf("Please enter the taps you'd like to use : ");//prompts user to input the taps and then makes sure theyre in range
while(TapInputs)
{
scanf("%d",&tap);
if((tap > 0)&&(tap < 33))
{
*(taps+count)=tap;
}
else if(tap == -1) // when we find -1 we do this
{
TapInputs = 0;
}
else if(tap > 32)
{
exit(0);
}
count++;
}
printf("How many numbers do you want to generate: "); //prompts user to input the number of numbers to use
scanf("%d", &myNewNumber);
while (myNewNumber < 0)// error checking for positive inputs
{
printf("How many numbers do you want to generate: ");
scanf("%d", &myNewNumber);
}
printf("\nRandom Numbers:");
while(myNewNumber)//creates number equal to the user input number in the previous step
{
temp[0] = mySeed; // makes temp1 the seed
temp[1] = mySeed; // makes temp2 the seed
temp[0] = (temp[0] >> taps[0]) & 1; // checks and sets the bit
temp[1] = (temp[1] >> taps[1]) & 1; // checks and sets the bit
myToggle = (temp[0] ^ temp[1]); // here we xor the temp1 and 2
mySeed = (mySeed << 1) ^ myToggle; // use bittoggle to shift the seed and generate a new number
fprintf(fp, "%d\r\n", mySeed); // wrties the generated number into the file
printf("\n%d", mySeed); // prints the number
myNewNumber -= 1;
}
fclose(fp); // closes file, creates a new line and returns 0 to the fucntion
printf("\n");
return 0;
}
else
{ // if the number the user input was 0 we will end our program
exit(0);
}
}
The fault happens immediately upon execution.
This piece of code :
while(TapInputs)
{
scanf("%d",&tap);
if((tap > 0)&&(tap < 33))
{
*(taps+count)=tap;
}
else if(tap == -1) // when we find -1 we do this
{
TapInputs = 0;
}
else if(tap > 32)
{
exit(0);
}
count++;
}
is executed until you find TapInputs to be false, or in other words 0. This will happen only if you give -1 as an input to scanf("%d", &tap). Until then, you will keep reading and also incrementing count.
But some lines above, you have declared
unsigned int taps[2];
and in your while loop you do
*(taps+count)=tap;
So, if you have read tap enough times and keep finding it between 0 and 33, until you find it -1, count will have increased enough to get your array out of bounds.
I think problem is in this line , mySeed = atoi(argv[1]);
you have to do something like this,
you can put the code from that in an if condition ,
if(agrc>1)
{
mySeed = atoi(argv[1]);
---------------------
--------------------
}
I have tested...it is working
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
unsigned int mySeed; // creates our variables
unsigned int taps[2];
unsigned int temp[2];
unsigned int myToggle;
FILE *fp;//opens the file
fp = fopen("random.txt", "w");//sets the file to open equal to this file
int TapInputs = 1;
int count = 0;
int tap;
int myNewNumber = 0;
if(agrc>1)
{
mySeed = atoi(argv[1]);
if(atoi(argv[1]) > 0) //Error checking for negative inputs.
{
printf("Please enter the taps you'd like to use : ");//prompts user to input the taps and then makes sure theyre in range
while(TapInputs)
{
scanf("%d",&tap);
if((tap > 0)&&(tap < 33))
{
*(taps+count)=tap;
}
else if(tap == -1) // when we find -1 we do this
{
TapInputs = 0;
}
else if(tap > 32)
{
exit(0);
}
count++;
}
printf("How many numbers do you want to generate: "); //prompts user to input the number of numbers to use
scanf("%d", &myNewNumber);
while (myNewNumber < 0)// error checking for positive inputs
{
printf("How many numbers do you want to generate: ");
scanf("%d", &myNewNumber);
}
printf("\nRandom Numbers:");
while(myNewNumber)//creates number equal to the user input number in the previous step
{
temp[0] = mySeed; // makes temp1 the seed
temp[1] = mySeed; // makes temp2 the seed
temp[0] = (temp[0] >> taps[0]) & 1; // checks and sets the bit
temp[1] = (temp[1] >> taps[1]) & 1; // checks and sets the bit
myToggle = (temp[0] ^ temp[1]); // here we xor the temp1 and 2
mySeed = (mySeed << 1) ^ myToggle; // use bittoggle to shift the seed and generate a new number
fprintf(fp, "%d\r\n", mySeed); // wrties the generated number into the file
printf("\n%d", mySeed); // prints the number
myNewNumber -= 1;
}
fclose(fp); // closes file, creates a new line and returns 0 to the fucntion
printf("\n");
return 0;
}
else
{ // if the number the user input was 0 we will end our program
exit(0);
}
}
}
I recently wrote a C program for the following 'Seven thieves and diamonds' puzzle:
"There are seven thieves, They steal diamonds from a diamond merchant and run away in jungle. While running, night sets in and they decide to rest in the jungle When everybody’s sleeping, two of the best friends get up and decide to distribute the diamonds among themselves and run away. So they start distributing but find that one diamond was extra. So they decide to wake up 3rd one and divide the diamonds again …..only to their surprise they still find one diamond extra. So they decide to wake up fourth one. Again one diamond is spare. 5th woken up……still one extra. 6th still one extra. Now they wake up 7th and diamonds are distributed equally."
Although the logic is quite simple to understand, my program seems to be quite buggy. It only seems to run for the numbers 3, 5 and 7.
I am new to programming in general and I feel that my program is not very sophisticated:
#include<stdio.h>
int main()
{
int n,i,j,k;
int a[30];
printf("Enter the number of thieves\n");
scanf("%d",&n);
i=n+1;
while(1)
{
j=2;
k=0;
while(j<n)
{
if(i%j == 1 && i%n==0)
{
a[k]=1;
}
else
{
a[k]=0;
}
if(k==n-2)
{
k=0;
}
j++;
k++;
}
for(j=0;j<n-1;j++)
{
if(a[j]==0)
{
break;
}
else if(j==n-3 && a[j] == 1)
{
printf("The number of diamonds = %d\n",i);
return;
}
}
i++;
}
}
It would be great if someone could help me develop this code into something more nonspecific, such that it could return an output for all values of 'n.' Also, any feedback in general would be highly appreciated.
Your code is very hard to follow, so I wrote my own code to debug this and your program although obscure and hard to follow is completely correct for valid inputs, you are just not handling all the cases very well thus you are in a while loop forever. Not every input will work for this problem only prime numbers will give you an answer for this problem so inputs like 2, 4, and 6 will not work so they need to be handled.
Here is a test comparing your outputs with the test I wrote for valid inputs.
#Of Theives Your Code Test Code
3 3 3
5 25 25
7 301 301
11 25201 25201
13 83161 83161
You can write a quick function to test for this care of this like this:
int isPrime(int tmp)
{
int i;
for(i = 2; i <= tmp/2; i++)
{
if(tmp % i == 0)
return 0;
}
return 1;
}
Then you can check for valid inputs which numbers greater than 1 (because then there would not be enough thiefs for the story to happen) and prime like so:
#include<stdio.h>
int isPrime(int tmp)
{
int i;
for(i = 2; i <= tmp/2; i++)
{
if(tmp % i == 0)
return 0;
}
return 1;
}
int main()
{
int n,i,j,k;
int a[30];
printf("Enter the number of thieves that is prime and greater than 1\n");
scanf("%d",&n);
i=n+1;
if(isPrime(n) && n > 1)
{
while(1)
{
j=2;
k=0;
while(j<n)
{
if(i%j == 1 && i%n==0)
{
a[k]=1;
}
else
{
a[k]=0;
}
if(k==n-2)
{
k=0;
}
j++;
k++;
}
for(j=0;j<n-1;j++)
{
if(a[j]==0)
{
break;
}
else if(j==n-3 && a[j] == 1)
{
printf("The number of diamonds = %d\n",i);
return;
}
}
i++;
}
}
else
{
printf("Input Invalid.\n");
}
}
The code I wrote to test the riddle:
#include<stdio.h>
int isPrime(int tmp)
{
int i;
for(i = 2; i <= tmp/2; i++)
{
if(tmp % i == 0)
return 0;
}
return 1;
}
long gcd(long a, long b) {
if (b == 0) {
return a;
}
else {
return gcd(b, a % b);
}
}
int main()
{
int thieves, i;
long diamonds, lcm = 1;
printf("Enter the number of thieves that is prime and greater than 1:\n");
scanf("%d",&thieves);
if(isPrime(thieves) && thieves > 1)
{
for(i = 2;i < thieves;i++)
{
lcm = (lcm*i)/gcd(i,lcm);
}
i = 1;
dimonds = lcm*i + 1;
while(dimonds % thieves != 0)
{
dimonds = lcm*++i + 1;
}
printf("There are a minimum of diamonds is: %d\n",diamonds);
}
else
{
printf("Input inv\n");
}
}
iI'm trying to understand how to properly use arrays in C and tried to write a simple program, which is supposed to take a 5-integers array and eliminate zeroes to the left. This is my attempt:
#include <stdio.h>
int main() {
int seq[5];
int i;
int cor[5];
int counter;
printf("Type the 5 numbers: ");
scanf("%s", &seq);
for (i=0; i<5; i++){
if (seq[i] != 0) {
for (counter=0; counter<5-i; counter++){
cor[counter]=seq[i+counter];
}
break;
}
}
printf("%s", cor);
return 0;
}
The idea was that when something such as 00101 was entered, the program would look at each entry and check whether it is 0. If it isn't, at the position i, it would write a new array in which the 0-th position is assigned the value of the original array at i, the 1-th position would be assigned the value at i+1 and so on, and then it would print this new array, which should have no useless zeroes to the left. But it just prints the original array. What is wrong? Sorry for the begginner's question.
You have two ways to do what you want.
using chars :
#include <stdio.h>
int main() {
char seq[6]; /* reserve one place for terminating null */
int i;
char cor[5];
int counter;
printf("Type the 5 digits : ");
scanf("%5s", seq); /* limit to 5 chars and seq is already an array : ne & */
for (i=0; i<5; i++){
/* should control seq[i] >= '0' && seq[i] <= '9' */
if (seq[i] != '0') {
for (counter=0; counter<6-i; counter++){ /* also copy terminating null */
cor[counter]=seq[i+counter];
}
break;
}
}
printf("%s", cor);
return 0;
}
but user could give you chars that are not digits.
using ints :
#include <stdio.h>
int main() {
int seq[5];
int i;
int cor[5];
int counter;
printf("Type the 5 numbers: ");
i = scanf("%d%d%d%d%d", seq, seq+1, seq+2, seq+3, seq+4); /* seq+i = &(seq[i]) */
/* should control i == 5 */
for (i=0; i<5; i++){
if (seq[i] != 0) {
for (counter=0; counter<5-i; counter++){
cor[counter]=seq[i+counter];
}
break;
}
}
printf("%d%d%d%d%d", cor[0], cor[1], cor[2], cor[3], cor[4]);
return 0;
}
You cannot store a string in an integer array. You have to store strings in character arrays.
The question is that show the digits which were repeated in C.
So I wrote this:
#include<stdio.h>
#include<stdbool.h>
int main(void){
bool number[10] = { false };
int digit;
long n;
printf("Enter a number: ");
scanf("%ld", &n);
printf("Repeated digit(s): ");
while (n > 0)
{
digit = n % 10;
if (number[digit] == true)
{
printf("%d ", digit);
}
number[digit] = true;
n /= 10;
}
return 0;
}
But it will show the repeated digits again and again
(ex. input: 55544 output: 455)
I revised it:
#include<stdio.h>
int main(void){
int number[10] = { 0 };
int digit;
long n;
printf("Enter a number: ");
scanf("%ld", &n);
printf("Repeated digit(s): ");
while (n > 0)
{
digit = n % 10;
if (number[digit] == 1)
{
printf("%d ", digit);
number[digit] = 2;
}
else if (number[digit] == 2)
break;
else number[digit] = 1;
n /= 10;
}
return 0;
}
It works!
However, I want to know how to do if I need to use boolean (true false), or some more efficient way?
To make your first version work, you'll need to keep track of two things:
Have you already seen this digit? (To detect duplicates)
Have you already printed it out? (To only output duplicates once)
So something like:
bool seen[10] = { false };
bool output[10] = { false };
// [...]
digit = ...;
if (seen[digit]) {
if (output[digit])) {
// duplicate, but we already printed it
} else {
// need to print it and set output to true
}
} else {
// set seen to true
}
(Once you've got that working, you can simplify the ifs. Only one is needed if you combine the two tests.)
Your second version is nearly there, but too complex. All you need to do is:
Add one to the counter for that digit every time you see it
Print the number only if the counter is exactly two.
digit = ...;
counter[digit]++;
if (counter[digit] == 2) {
// this is the second time we see this digit
// so print it out
}
n = ...;
Side benefit is that you get the count for each digit at the end.
Your second version code is not correct. You should yourself figured it out where are you wrong. You can try the below code to print the repeated elements.
#include<stdio.h>
int main(void){
int number[10] = { 0 };
int digit;
long n;
printf("Enter a number: ");
scanf("%ld", &n);
printf("Repeated digit(s): ");
while (n > 0)
{
digit = n % 10;
if (number[digit] > 0)
{
number[digit]++;;
}
else if (number[digit] ==0 )
number[digit] = 1;
n /= 10;
}
int i=0;
for(;i<10; i++){
if(number[i]>0)
printf("%d ", i);
}
return 0;
}
In case you want to print the repeated element using bool array (first version) then it will print the elements number of times elements occur-1 times and in reverse order because you are detaching the digits from the end of number , as you are seeing in your first version code output. In case you want to print only once then you have to use int array as in above code.
It is probably much easier to handle all the input as strings:
#include <stdio.h>
#include <string.h>
int main (void) {
char str[256] = { 0 }; /* string to read */
char rep[256] = { 0 }; /* string to hold repeated digits */
int ri = 0; /* repeated digit index */
char *p = str; /* pointer to use with str */
printf ("\nEnter a number: ");
scanf ("%[^\n]s", str);
while (*p) /* for every character in string */
{
if (*(p + 1) && strchr (p + 1, *p)) /* test if remaining chars match */
if (!strchr(rep, *p)) /* test if already marked as dup */
rep[ri++] = *p; /* if not add it to string */
p++; /* increment pointer to next char */
}
printf ("\n Repeated digit(s): %s\n\n", rep);
return 0;
}
Note: you can also add a further test to limit to digits only with if (*p >= '0' && *p <= '9')
output:
$./bin/dupdigits
Enter a number: 1112223334566
Repeated digit(s): 1236
Error is here
if (number[digit] == true)
should be
if (number[digit] == false)
Eclipse + CDT plugin + stepping debug - help you next time
As everyone has given the solution: You can achieve this using the counting sort see here. Time complexity of solution will be O(n) and space complexity will be O(n+k) where k is the range in number.
However you can achieve the same by taking the XOR operation of each element with other and in case you got a XOR b as zero then its means the repeated number. But, the time complexity will be: O(n^2).
#include <stdio.h>
#define SIZE 10
main()
{
int num[SIZE] = {2,1,5,4,7,1,4,2,8,0};
int i=0, j=0;
for (i=0; i< SIZE; i++ ){
for (j=i+1; j< SIZE; j++){
if((num[i]^num[j]) == 0){
printf("Repeated element: %d\n", num[i]);
break;
}
}
}
}