Questions tagged [variables]
The variables tag has no usage guidance.
43 questions
0
votes
1
answer
60
views
Error Message while doing Greedy for declared variable
greedy.c:8:7: note: previous definition is here
int i;
This variable has been declared and used and I am getting this error message.
If I remove the declared variable then it tells me I didn't ...
0
votes
2
answers
203
views
pset1 mario Initializing for loop from GetInt
I'm running the Mario program and I have everything working except the correct count, which I can fix. The only issue I'm having is a syntactical one. I'm tying to initialize the "for loop" with the ...
0
votes
1
answer
19
views
How do I combine variables and words in a sentance?
printf("%i\n", quarters, "quarters,", "%i\n", dimes, "dimes,", "%i\n", nickels, "nickels", "%i\n", pennies, "pennies.");...
0
votes
1
answer
27
views
pointers and addresses
int m=10;
int *ab=&m;
printf("address of *ab:%p\n",&*ab);
printf("adress of ab:%p\n",&ab);
output:
address of *ab:0x7ffd85fddefc
adress of ab:0x7ffd85fddf00
i ...
0
votes
1
answer
50
views
Problem with pointers, getting seg fault while indexing with variable instead of plain number
I would like to ask why is that giving me seg fault.
int **a = (int**) malloc (sizeof(int*) * 3);
a[0][0] = 1;
a[0][1] = 2;
a[0][2] = 3;
for(int i = 0; i < 3; i++)
{
printf("%d",...
1
vote
2
answers
52
views
Fundamental issue to understand functions with python ===> Impact on all problems
I feel that this is a wrong question to ask because it is simply to wide, and as per my understanding, questions should always be very concise and precise.
But I have a lingering issue on the notion ...
1
vote
1
answer
285
views
Declaring a variable in loop or outside
I remember when David talked about declaring variables, he said we couldn't say like 'Hey computer, give me a variable called n' and then later say 'oh hey, give me another variable called n'. The ...
0
votes
1
answer
69
views
pset5 speller Hash table not appearing in the debug50 stack/heap
This question is related with this other question of mine, in which I am trying to complete pset5: speller.
I took Cliff's advice, and additionally executed the debug50 on my code. Since the original ...
1
vote
1
answer
70
views
Global Variable vs Local variables?
I'm trying to write a function that will be called in main(int argc, string argv[]) for pset 2: plurality.
Inside the function, I want a for loop. I've written this:
bool vote(string name)
{
...
0
votes
1
answer
55
views
Problem set 1 Mario variable issue
I've been staring at my computer for so long trying little tweeks to fix the error message. TLDR how do I stop the shadow message from appearing and initialize variable h? The image looks confusing ...
0
votes
1
answer
38
views
Substring SQL Search
I am trying to query the Database for a sub-string, I am substituting a string variable from a form for the query. It is giving me a syntax error.
This is the error:
LINE 1: SELECT * FROM books ...
0
votes
1
answer
62
views
How do you make a variable work in multiple loops?
I am trying to solve the vigenere.c problem, but there is one step that I need to figure out before my code works. I am stuck here and I don't know what else I could do. In my code, I have a variable ...
0
votes
1
answer
41
views
Pset7 Sell stockprice not a bound parameter
I'm working on sell, and I ran into trouble in one of my db.execute statements. For whatever reason, in the previous line, it was perfectly fine using stock["price"] but in my second db.execute, it ...
0
votes
1
answer
50
views
Printing a character times a variable
I have a question about that I would greatly appreciate if someone with knowledge could answer. In C, is it valid to print a character times a variable?
For example (spaces and stories are both ...
3
votes
2
answers
2k
views
How to Declare and Initialize Variables in C?
While I was watching the walkthrough for pset2 (capitalize), I noticed that David didn't declare a variable in his program. However, it still worked and I was wondering why.
I tried to implement my ...
0
votes
1
answer
34
views
Pset2 crack variable changing value for no apparent reason
I'm working on crack and have been messing around with strcmp(), but something isn't working as expected. When I print s (with argv[1] != "hello") it returns the right value, but somehow between the ...
0
votes
1
answer
317
views
pset1: cash: How to deal with undeclared variables?
A warning: I know there must be an easier way to make this script work. But I'm a newb who has a very hard time wrapping my head around programming and I don't want to destroy my thinking by looking ...
0
votes
1
answer
67
views
Hello, I have a problem with the water consumption code in pset 1
I have a problem with the water consumption code in pset 1.
#include <cs50.h>
#include <stdio.h>
int main(void)
{
printf("Minutes:");
int n = get_int();
}
An error ...
0
votes
1
answer
490
views
Pset4 Recover not working
I am working on pset4's recover. My variable, outptr, is not being recognized by other parts of my do-while loop. Why is this?
#include <stdio.h>
#include <stdlib.h>
#include "...
0
votes
1
answer
54
views
scope of local variables
If I run following code, I get output
From inside innermost braces c is 5
from just outside innermost brace, c is 8
from inside function 48
From just before end of main c is 8
But if I change ...
1
vote
2
answers
38
views
C: Pset 3: Binary Search - 'expected an exit code of 0, not 1' (not finding n)
This is an attempt at binary search. n is the user input (from main script).
This code compiles but doesn't find the needle in the haystack. I'm a bit lost and would welcome assistance.
bool ...
0
votes
2
answers
150
views
Error when assigning a structure to another of the same type
It seems like the following should copy t1 into t3 (that seems to be what the documentation I have read suggests)
#include<stdio.h>
struct test {
int a;
char* name;
struct test* b;
};
...
0
votes
1
answer
74
views
What does "using" a variable entail and why does this inconsistency come up? (pset1, greedy, general question)
During the greedy problem in pset1 I wrote this function:
void greedy(int coin_type, int *change, int *coins) {
while (*change / coin_type > 0) {
*coins++;
*change -= coin_type;...
0
votes
1
answer
104
views
Multiplication function error?
I'm getting an error for the value of c when I test this code... when I input '2.10' as the x value, c= x*100 gives me a value of 209?? For other values it seems to work so far, weird. I'm new to ...
0
votes
2
answers
1k
views
global variables and macros
if we use
#define a 1 - MACROS
or int a = 1 - Global variables before main function
what is the difference between the both of them assuming I won't alter the value of global ...
0
votes
1
answer
23
views
calculations vs variables performance
In recursive scenarios, what brings more performance? Doing 3-4 simple calculations once, store result on a variable and then use variable maximum 3-4 times in each recursive function call? Or to skip ...
0
votes
1
answer
1k
views
Use of functions and variable declarations outside main() on pset1 greedy
I have completed greedy from pset1 and I still doubts about the variable declarations and the function position.
What is the difference if they are declared inside or outside the main function?
#...
0
votes
2
answers
161
views
Whats's difference between declaring and defining variables?
According to lectures all variables we use are declared as
int x = something;
or
int x;
But in pset3..in files that Cs50 staff made,they have done;
#define DIM_MIN 3 and #define DIM_MAX 9
So ...
0
votes
1
answer
226
views
Can I create an element of arbitrary size in a STRUCT?
I'm working on recover.c.
It seems like a tidy solution to eliminate fseek calls would be to create a struct like the following:
typedef struct
{
BYTE first;
BYTE second;
BYTE third;
...
0
votes
1
answer
582
views
Difference between Pass by Value and Pass by Reference (a practical example)
at the end of Doug's video on arrays he explains how variables get passed in function calls in comparison to how arrays work.
void set_array(int array[4]);
void set_int(int x);
int main (void)
{
...
0
votes
1
answer
2k
views
How can I pass an array or a long long unsigned variable as an argument to a function?
Okay, I admit I have no business doing the "hacker edition" problems, but I am having so much fun with this that I want to do ALL THE PROBLEMS!
Anyway. I'm working on pset1 hacker edition "credit."
...
0
votes
2
answers
161
views
Error using string as a data type while including the cs50 library
I am receiving the following error while compiling my code:
jharvard@appliance (~/Dropbox/pset2): make initials
clang -ggdb3 -O0 -std=c99 -Wall -Werror initials.c -lcs50 -lm -o initials
initials....
0
votes
0
answers
26
views
Issue Initializing variable
I hope someone can help clear this up for me.
If I use this I get an error:
int i = GetInt();
https://gist.github.com/anonymous/7707d2b851d40106c525#file-gistfile1-txt
I get an error... but if I ...
0
votes
1
answer
26
views
How do I use my own function for ball movement and call it in main, rather than writing it in main
I've successfully written code inside the while loop in main that makes the ball bounce correctly. However, first I tried to call a function from main (just to make it easier to read), called ...
1
vote
1
answer
375
views
Pset2 caesar unused variable
Hello and thanks for reading.
I have created a few functions and at the moment I have no idea if my code works, so I just want to test to see if my code works but I am getting a weird error which I'...
0
votes
1
answer
385
views
compilation error multi-character character constant in caesar
The code is:
string plaintext = GetString();
for (int i = 0, n = strlen(plaintext); i < n; i++)
{
printf("%c", plaintext[i]);
if ((isalpha(plaintext[i])) && (isupper(...
5
votes
3
answers
288
views
what happens to memory when a variable dies?
Whenever a variable dies, the memory is erased. But is the content of the memory erased? And if so what happens to the contents of the memory ? Why do we see garbage values (that many times does not ...
0
votes
1
answer
906
views
Get more than one inputs in same line without pressing enter key
I want to make a calculater that takes input like
23 + 24
The two ints or floats plus the character in the same line
Tried
float a = Getfloat();
char c = GetChar();
float b = Getfloat();
With this ...
1
vote
1
answer
51
views
Why does this check for equality fail to trigger in my Scratch project?
I attempted to make a basic game where you should click the Dragon and not the girl. I wanted to make it so that when you click the girl you lose a health point and when your health point reaches 0 ...
2
votes
1
answer
154
views
Global Variables vs #defined Constants
Every time they talk about global variables, it's generally considered "bad practice" to use them unless there is a compelling reason to.
#defined CONSTANTS
on the other hand, seem to be fair game.
...
3
votes
1
answer
18k
views
How to initialize multiple types of variables in a FOR loop?
Is there a way to initialize two different types of variables in a for loop? For example:
for (int i = 0, n = strlen(oldStr), char a, b; i < n; i++)
So the variables i and n are ints while a and ...
0
votes
1
answer
76
views
How on Earth can this variable change without any commands? Please help!
As you can see, the value of buffer[0] changes when the line sprintf(tittle, %03d.jpg", images);, which has nothing to do with the buffer. All of the values in buffer are changed. I went through my ...
0
votes
2
answers
80
views
Multiple byte storage and comparison
How should I by using these bytes(spaces in between each bytes?) and What type of variable would nf need to be in this context?
if(nf == 0xff 0xd8 0xff 0xe0 || nf == 0xff 0xd8 0xff 0xe1)