For this assignment I am supposed to use the debugger in Visual Studios, I kind of understand how to use it but i cant figure it out completely. For the first part of the code, it asks for all values of a,b, and c. I put my debugger to start right before it starts and then right after it ends. I run it through using the F10 key. I go through it once and I get a=6, b=3, c=6. Then once I go through it again i get different values, however I want to view those values as a list. Is that possible?
//THIS IS THE CODE.
#include<stdio.h>
void func1(int a, int b, int c)
{
//Track all values of a, b, and c
printf("%d %d %d\n",a,b,c);
a = b + b;
printf("%d %d %d\n",a,b,c);
}
int func2(int x)
{
//Track all values of x
printf("%d\n",x);
for (x = 7; x < 12; x += 1)
{
printf("%d", x + 10);
}
return(x);
}
int func3(int x)
{
x = x + 51;
return(x);
}
int main()
{
int a,b,c;
//Track each array index value
int arr[5];
a = 7;
b = 3;
c = 3;
func1(5, 3, 6);
func2(c);
b = func2(c);
for (c = 0; c < 5; c += 1)
{
arr[c] = c + 2;
}
for (b = 22; b > 7; b += -1)
{
arr[b + func3(a)] = a + b + c;
printf("%d\n", b);
}
for (b = 0; b < 5; b += 1)
{
printf("%d\n", arr[b]);
}
return(0);
}
arr[b + func3(a)]
Before debugging further fix the bug in the code.
Here you have array out of bound access which will lead to undefined behavior.
You are trying to access arr[22 + x] where as the size of the array is 5.
Related
Currently on one of the cs50x problem sets 'Cash', which is a simple 'ask for how much change is owed, then calculate how many coins are required' task, so not here asking for a solution but, I don't understand why this won't work.
While it does ask for an input, when I type in a float such as 5.96, it simply hangs. No returns, no errors whatsoever. I have to force it shut. The other thing is the while loop doing the same when set to 0, which is the intended way of doing things in order to get the exact number of coins.
I know how inefficient this code is and there are simpler ways of doing things. I just wish to understand the whys in order to avoid making the same mistakes moving on. Thanks.
#include <stdio.h>
#include <cs50.h>
#include <math.h>
int main(void)
{
// changes and other containers
int p = 1;
int n = 5;
int d = 10;
int q = 25;
int x = 0;
float c;
// get how much change is owed in float
do
{
c = get_float("Change owed: ");
}
while (c < 0);
// int conversion to avoid imprecision
int a = round(c * 100);
// 1 because 0 spits out an unknown error
while (a >= 1)
{
// if the converted amount is bigger than a quarter
if (a >= q)
{
// x = number of coins, a = amount left
x = a / q;
a = a % q;
}
else if (a >= d)
{
x = x + a / d;
a = a - a % d;
}
else if (a >= n)
{
x = x + a / n;
a = a - a % n;
}
else
{
x = x + a / p;
a = a - a % p;
}
}
printf("%i\n", x);
printf("%i\n", a);
}
Thanks to WhozCraig, I figured out that my logic was at fault.
#include <stdio.h>
#include <cs50.h>
#include <math.h>
int main(void)
{
// changes and other containers
int p = 1;
int n = 5;
int d = 10;
int q = 25;
int x = 0;
float c;
// get how much change is owed in float
do
{
c = get_float("Change owed: ");
}
while (c < 0);
// int conversion to avoid imprecision
int a = round(c * 100);
// 1 because 0 spits out an unknown error
while (a >= 1)
{
// if the converted amount is bigger than a quarter
if (a >= q)
{
// x = number of coins, a = amount left
x = a / q;
a = a % q;
}
else if (a >= d)
{
x = x + a / d;
a = a % d;
}
else if (a >= n)
{
x = x + a / n;
a = a % n;
}
else
{
x = x + a / p;
a = a % p;
}
}
printf("%i\n", x);
printf("%i\n", a);
}
What is the problem with this code?
#include <stdio.h>
#include <stdlib.h>
#include "time.h"
void createNumb(int RandomNumber) {
srand(time(NULL));
int a, b, c, d;
a = rand() % 10 + 0;
b = rand() % 10 + 0;
c = rand() % 10 + 0;
d = rand() % 10 + 0;
while (b == a) {
b = rand() % 10 + 0;
}
while (c == b || c == a) {
c = rand() % 10 + 0;
}
while (d == c || d == b || d == a) {
d = rand() % 10 + 0;
}
RandomNumber = ("%d%d%d%d", a * 1000 + b * 100 + c * 10 + d * 1);
}
int main() {
int x;
createNumb(x);
printf("%d", x);
}
When I run this program, it always give me the result 0.
I want it to print a random 4-digit number (each digit must be unique). What is the problem?
Your createNumb function returns void instead of an int.
You should use srand() only once, so it’s better move it to your main.
Your createNumb function should return a random integer, like this:
int createNumb(void) {
/*
** Simplified for readability.
** Here you would use your function that generates 4 different digits
*/
return rand();
}
int main(void) {
srand(time(NULL))(
int x = createNumb();
printf("%d" , x);
}
Or, if you want createNumb to fill an existing integer, use a pointer:
void createNumb(int *randomNumber) {
/*
** Simplified for readability.
** Here you would use your function that generates 4 different digits
*/
*randomNumber = rand();
}
int main(void) {
int x;
srand(time(NULL))
createNumb(&x); // Passing a pointer to x
printf("%d" , x);
}
There are some problems in your C code, coming from a different language:
use return to return a value from a function and specify the return type before the function name`.
our attempt at formating the number is incorrect and unneeded. Incidentally, RandomNumber = ("%d%d%d%d", a * 1000 + b * 100 + c * 10 + d * 1); uses the operator , that ignores its left operand and evaluates to the right operand, whose value is stored into a local variable with automatic storage duration, and is lost upon function exit. Just use return a * 1000 + b * 100 + c * 10 + d * 1;.
to compute a 4 digit pseudo-random value, you could just use rand() % 10000, but an approach such as your may be needed for more digits or for specific constraints such as unique digits.
to make your results more random, use a faster changing source for srand() such as clock() and initialize the pseudo-random number generator just once int the main() function.
you could use the printf format "%04d" to make display an initial 0 if the result is less than 1000.
Here is a modified version:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int createNumb(void) {
int a, b, c, d;
a = rand() % 10;
b = rand() % 10;
c = rand() % 10;
d = rand() % 10;
while (b == a) {
b = rand() % 10;
}
while (c == b || c == a) {
c = rand() % 10;
}
while (d == c || d == b || d == a) {
d = rand() % 10;
}
return a * 1000 + b * 100 + c * 10 + d;
}
int main() {
int x;
srand(clock());
x = createNumb();
printf("%04d\n", x);
return 0;
}
Note that you can avoid the loops in creatNumb(). Here is a modified version that takes an argument from the command line to produce multiple results.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int createNumb(void) {
int a, b, c, d, bb, cc, dd;
a = rand() % 10;
b = rand() % 9;
c = rand() % 8;
d = rand() % 7;
bb = (b >= a);
cc = (c >= a) + (c >= b);
cd = (d >= a) + (d >= b) + (d >= c);
return a * 1000 + (b + bb) * 100 + (c + cc) * 10 + (d + dd);
}
int main(int argc, char *argv[]) {
int x, n;
srand(clock());
n = (argc > 1) ? strtol(argv[1], NULL, 0) : 1;
while (n-- > 0) {
x = createNumb();
printf("%04d\n", x);
}
return 0;
}
Boolean operators evaluate to 1 when they are true and 0 otherwise. bb = (b >= a); is a short notation for:
bb = 0;
if (b >= a)
bb = 1;
If you want to compute an array of 4 numbers with the given constraint, you should pass a pointer to the destination array as an argument to creatNumb(). Here is a modified version with this approach:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void createNumb(int *dest) {
int a, b, c, d, bb, cc, dd;
a = rand() % 10;
b = rand() % 9;
c = rand() % 8;
d = rand() % 7;
bb = (b >= a);
cc = (c >= a) + (c >= b);
cd = (d >= a) + (d >= b) + (d >= c);
dest[0] = a;
dest[1] = b + bb;
dest[2] = c + cc;
dest[3] = d + dd;
}
int main(int argc, char *argv[]) {
int array[4];
srand(clock());
createNumb(array);
printf("%d%d%d%d\n", array[0], array[1], array[2], array[3]);
return 0;
}
Note that passing an array as a argument to a function actually passes a pointer to its first element: createNumb(array); is equivalent to createNumb(&array[0]);. This process is referred to as arrays decay into pointers in most expression contexts, it is somewhat confusing for beginners but allows for efficient argument passing.
I am copying a program whose purpose is to generate and bubble sort a list of random numbers. This is the code:
#include <stdio.h>
#define MAX 10
int a;
int rand_seed=10;
int rand()
{
rand_seed = rand_seed * 1103515245 + 12345;
return (unsigned int)(rand_seed / 65536) % 32768;
}
void main()
{
int i,t,x,y;
//fill array
for (i = 0; i < MAX;i++)
{
a = rand();
printf("%d\n",a);
}
/* bubble sort the array */
for (x = 0; x < MAX-1; x++)
for (y = 0; y < MAX -x - 1; y++)
if (a > a[y+1]) {
t = a;
a = a[y+1];
a[y+1] = t;
}
printf("----------------\n");
for( i = 0; i < MAX;i++)
printf("%d\n",a);
}
I know it's offensive to post the whole thing here but I don't know where the problem is.
I believe your compiler already had the offensive line pointed out. In the code
if (a > a[y+1])
you're trying to use a as an array, while it was defined as an int, earlier
int a;
If you want a to be an array, you have to define it as an array, and populate each member using rand() or similar.
I am pretty sure you wanted a to be an array. In that case try this out:
int a[10];
for (i = 0; i < MAX;i++) {
a[i] = rand();
printf("%d\n",a[i]);
}
And also this part would be changed into:
if (a[y] > a[y+1]) {
t = a[y];
a[y] = a[y+1];
a[y+1] = t;
}
while running this code I am getting floating point exception pls explain why it is coming so.
#include <stdio.h>
int gcd(long int, int);
int main() {
int t, n, a, i;
long int abc;
scanf("%d", &t);
while (t--) {
abc = 1;
scanf("%d", &n);
abc = n * (n - 1);
for (i = n - 2; i > 1; i--) {
a = gcd(abc, i);
abc = ((abc * i) / a);
}
printf("%ld \n", abc);
}
return 0;
}
int gcd(long int a, int b) {
if (b == 0)
return a;
else {
return (b, a % b);
}
}
The else part in gcd function is bogus. You probably wanted to call gcd recursively and instead you are returning a % b. And as a result if a % b == 0 you divide by 0 on line 13.
The expression (b, a % b) is evaluated as two subexpressions separated by comma operator. The value of b is forgotten and the value of the whole expression becomes a % b.
The correct version:
int gcd(long int a, int b) {
if (b == 0)
return a;
else {
return gcd(b, a % b);
}
}
You should change a few things:
1) if(a == 0) instead of b==0 and return variable b
2) Use recursion gcd(b%a, a)
if (a == 0)
return b;
else {
return gcd(b%a, a);
}
And you can also use this short version
return a ? gcd(b%a,a) : b;
Proof:
We have m = n * (m div n) + m Mod n.
This equation is correct even if n = 1, because m div 1 = m and m mod 1 =0.
If n and m mod n it's multiple of d, also m is multiple of d, because n * (m div n) and m mod n / d.
On the other hand if m and n is multiple of e, to because m mod n = m -n * (m div n) is also multiple of e.
how to get the value of an integer x, indicated by x!, it is the product of the numbers 1 to x.
Example: 5! 1x2x3x4x5 = 120.
int a , b = 1, c = 1, d = 1;
printf("geheel getal x = ");
scanf("%d", &a);
printf("%d! = ", a);
for(b = 1; b <= a; b++)
{
printf("%d x ", c);
c++;
d = d*a;
}
printf(" = %d", d);
how to get the som of an integer x, indicated by x!, is the product of the numbers 1 to x.
Did you mean factorial of x ?
Change d = d*a; to d = d*b inside the loop
You can simply do:
for(b = 1; b <= a; b++) {
d *= b;
}
// d now has a!
This is the optimal implementation in size and speed:
int factorial(int x)
{
static const int f[13] = { 1, 1, 2, 6, 24, 120, /* ... */ };
if ((unsigned)x < (sizeof f/sizeof f[0])) return f[x];
else return INT_MAX+1; /* or your favorite undefined behavior */
}
Hint: x! (x factorial) does not fit in an int except for very very small values of x.
Try
d = d * b;
instead of
d = d * a
and it should work fine
You actually have a lot of redundant code there, that might be why you did not spot the error yourself.
To calculate the factorial, you only need the accumulator (d in the above code) and the input (a). Why?
My code is not good as other but it works for me:
#include <iostream>
using namespace std;
unsigned int fattoriale (int n){
if (n == 1){
return 1;
}
else {
return n * fattoriale(n-1);
}
}
int main() {
int tmp, num;
cin >> num;
tmp = fattoriale(num);
cout << "Stampo il fattoriale del numero inserito: " << tmp << endl;
}
int factorial(int x)
{
int f;
if (x == 0)
{
f = 1;
}
else if (x > 0)
{
f = x*factorial(x-1);
}
return f;
}
int main()
{
int n = 0;
cout << factorial(n);
return 0;
}