Questions tagged [arrays]
The arrays tag has no usage guidance.
40
questions
1
vote
1
answer
61
views
Please help debug linear search program in C
Could you please help me debug a linear search program I wrote in C?
The do while loop seems to be executed only once without looping and, as a result, the program does not print the largest int as I ...
1
vote
1
answer
2k
views
What happens when you assign a character string to a char* instead of writing element by element?
I have a question about strings that hopefully someone can help with. I realise that doing something like:
char* word = malloc(4);
word = "THE";
is not really correct (as word is really a pointer to ...
1
vote
1
answer
31
views
Changing the size of an array
Overall goal statement :
Your goal is to create a function that removes the first and last
characters of a string. You're given one parameter, the original
string. You don't have to worry with ...
1
vote
1
answer
132
views
Pset2: Copying string contents
I just can't seem to get this right.
The example in the week 2 short Arrays is as follows:
int foo[5]{1,2,3,4,5};
int bar[5]
for(j=0;j<5;j++)
{
bar[j]=foo[j];
}
This does not seem to work with ...
1
vote
1
answer
82
views
Pset7 - Portfolio: $positions not filling
Working within index.php, I feel like I understand the logic of what is going on, and I can get portfolio.php to render out fine on the website - with the exception that it only prints out one row ...
1
vote
1
answer
43
views
Pset4 - Filter (more) Edge Detection Help
I'm testing my Gy function separately, and it looks like the values I put in the test array are not correctly showing up in the function. I printf() the main function and they are recording correctly, ...
1
vote
1
answer
679
views
Arrays of Pointers - implicit double dereferencing
Good day people!
The context is resize program in problem set 4, however, the question is general.
Following is how the main function begins for the relevant program:
int main(int argc, char *argv[])...
0
votes
1
answer
2k
views
PSET 3 Runoff Error: variable length array declaration not allowed at file scope
I realize that this code is not well designed, however I will try to solve that once my code can compile.
#include <stdio.h>
#include <cs50.h>
#include <string.h>
typedef struct
{
...
0
votes
1
answer
86
views
Arrays and their sizes in C
I am having problems related to arrays and their sizes in C. There's something I am doing wrong, but I don't know what exactly.
Here is a sample of the code:
#include <stdio.h>
#include <...
0
votes
1
answer
28
views
Selection Sort: Holes in Logic
#include <cs50.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
string s = get_string("Enter a number: ");
string answer ...
0
votes
1
answer
8
views
Problem understanding array with Caesar project
int main(int argc, string argv[])
{
string enteredtext = get_string("plain text:");
printf("Entered text by user: %s\n", enteredtext);
int n = strlen(enteredtext);
printf("...
0
votes
2
answers
93
views
Lecture 2 Arrays
I tried using the code David show to average a set of scores in an array.
#include <stdio.h>
#include <cs50.h>
float average (void);
int main(void)
{
int n = get_int("Total ...
0
votes
1
answer
144
views
Passing arrays into function in tideman
I've created a solution to week 3's tideman problem which is entirely logically correct. Unfortunately, I had to dodge the standard solution by commenting out parts of a function and altering its ...
0
votes
1
answer
42
views
runtime error in sandbox for array examples from week 2 lecture
This is confusing me a little and I want to fully understand it before moving through the rest of the lecture. I usually code alongside David's examples on screen and was trying to understand why this ...
0
votes
3
answers
658
views
I'm getting an for error expected expression. What exactly I'm I doing wrong?
I mainly want my else statement to work, but it's not working.
int main(int argc, string argv[])
{
if (argc != 2)
{
printf("Usage: ./caesar key\n");
}
else if (argc == 2)
{
for (int i = ...
0
votes
1
answer
107
views
Trouble modifying elements of an array of candidates in Plurality
I'm working on Plurality of pset3. I've run into an issue modifying the vote count of each candidate. I've isolated the problem in the full program below. It appears that
candidate zero = candidates[...
0
votes
1
answer
21
views
[pset2][Vinegere] Code not enciphering text after certain letters, please help!
#include<stdio.h>
#include<cs50.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
int main(int argc, string argv[])
{
if (argc > 2 || argc == 1)
{
...
0
votes
1
answer
135
views
Why string variable has no square bracket?
Though it's said that string is an array of characters why string variable does not have square brackets on the immediate right?
In CS50 we call string as
string s
why not as string[]?
then how can ...
0
votes
1
answer
59
views
Pset1: Error when trying to assign single digits of int variable to array using for loop
I'm currently working on Credit. I'm trying to find the first two digits of the card number using a for loop. I've created an array named firstdigits with two elements. Then I've created a for loop to ...
0
votes
1
answer
147
views
Error handling while running credit.c for pset1
I have broken the credit problem into 2 parts
Checking whether the sum after the Luhn’s algorithm has a zero at
the end or not
If the inout integer is valid then go ahead and find the card type
using ...
0
votes
1
answer
2k
views
Finding & printing character after space in string in C
I've been banging my head against a wall here for days, trying to print initials after user name input. The errors change continually and are many. I'm trying to name the string array 'name' then ...
0
votes
1
answer
155
views
PSET 4 Resize: Pointers, arrays and file IO
Stuck on PSET4 resize: the file I'm writing is going into overflow and I'm not sure why. I've designed it using nested for loops as you'll see.
It compiles and runs, but then it says the file is too ...
0
votes
1
answer
42
views
Clarification regarding for loops and arrays
In the continued lecture of week2:
At about 17:00 he writes the for loop. What does i stand for? I don't think it is age, but it can't be the number of students? What does it mean?
What is ages[i]? ...
0
votes
1
answer
10
views
Found a curious patterns in arrays, what is it?
In week 2, it was mentioned that the final integer value in array is 0, to know the string is done. I tried going past this, to see what would happen, with the following code:
#include <cs50.h>
...
0
votes
1
answer
15
views
CS50 Pset 2 Substitution - Array size errors
I'm using a for loop to assign chars to each element of an array one at a time. The array has been assigned a size and the for loop runs the same number of iterations as the array size, but I find ...
0
votes
1
answer
4k
views
How to find if element is not in an array
Does anyone know of an easy way in C to check if a an element is NOT in a given array?
I think it would be fairly easy to determine if an element IS in an array by looping through it and seeing if an ...
0
votes
2
answers
967
views
How to set loop limit on an array with unknown size
I have been thinking about looping through arrays whose sizes are not fixed in advance... for example, allowing a user to input as many elements into an array as they want.
Question 1: How would I set ...
0
votes
1
answer
55
views
I've been stuck on lab 2 Scrabble for a week now and can't seem to figure out where I've gone wrong as it shows error messages and doesn,t compile
#include <ctype.h>
#include <cs50.h>
#include <stdio.h>
#include <string.h>
// Points assigned to each letter of the alphabet
int POINTS[] = {1, 3, 3, 2, 1, 4, 2, 4, 1, 8, 5, ...
0
votes
2
answers
68
views
How to output each element of an array
In the Lecture 2 Short on Arrays, Doug Lloyd suggests that it would be a good practice problem to try to output each element of a 100-element array as an integer corresponding to its index position.
...
0
votes
1
answer
121
views
Question CS50 2021 Caesar - where is the bug?
I'm currently struggling with a bug in caesar pset2. If i use check 50 everything is green except:
:( encrypts "a" as "b" using 1 as key
output not valid ASCII text
:( ...
0
votes
1
answer
24
views
Understanding arrays, pointers with an example
/* day_mon1.c -- prints the
days for each month */
#include <stdio.h>
#define MONTHS 12
int main(void)
{
int days[MONTHS = {31,28,31,30,31,30,31,31,30,31,30,31};
int index; for (index = 0;
...
0
votes
1
answer
44
views
pset2 Readability: reading index being calculated incorrectly even though hard sentence, word and letter numbers are correct
The indexes come out sometimes too low,some times too high and I don't really understand why if the variable calculations come out right every time.
Here's my code for it:
//calculates average letters ...
0
votes
1
answer
82
views
Runoff pset 3, arrrays and indexing
I don't understand what's going on with the solutions to the voter preference and tabulate functions. I had set up a similar function to the answer with the exception that I had
[voter][i] = rank;
...
0
votes
1
answer
91
views
Try to use += on string to stack char(s) in C
I know it sounds real stupid, but It is cuz I just lightly learned Java and Python last year...
now I am try to do C properly with CS50.
on week 2, there is Caesar encipher text program task.
I ...
0
votes
1
answer
167
views
Question for Array in C (with function e.g. set_array)
While I was doing cs50 week2.
I found something hard to understand.
#include<stdio.h>
void set_array(int array[4]);
void set_int(int x);
int main(void)
{
int a = 10;
int b[4] = {0, 1,...
0
votes
1
answer
66
views
PSET4 recover! dynamically allocated array does not seem to work
Hope all is well.
This is probably a stupid question but I am going to ask it anyway.
I have finally finished Recover (PSET4). I initially dynamically allocated an array as follows to store my 512 ...
0
votes
1
answer
40
views
Having some issues understanding how pointers work and how to pass them to functions and use them
int* create_random_array(int n){
int arr[n];
srand(0);
for (int i = 0; i < n; i++)
{
// Fill with random ints 1-100
arr[i] = (rand() % 100)+1;
}
return arr;...
0
votes
1
answer
66
views
handlign multidimesional arrays as function arguments and return values
So I stuck at the pset3, at the place where you need to implement game of fifteen. How can I pass multidimensional array as an function argument? And how can I return multdimesional array as a ...
0
votes
0
answers
154
views
Why can't I create an array of strings?
I have this habit of first trying out all the exercises of the book then going on to psets. I am on chapter 7, arrays. the question was to make a program of some sort of voting counter and instead of ...
0
votes
1
answer
33
views
Array initialilizer must be an intitializer list or string literal
Given this text:
include
include
int main (void)
{
string text = get_string("Text: ");
char num[] = text;
}
When this is run, it gives the error as stated in the heading of this question
...