I've got a problem with something I'm programming.
I get this error over and over;
jharvard#appliance (~/Dropbox/pset1): make mario
clang -ggdb3 -O0 -std=c99 -Wall -Werror mario.c -lcs50 -lm -o mario
mario.c:23:5: error: expected identifier or '('
do
^
mario.c:32:1: error: expected identifier or '('
do
^
2 errors generated.
I searched all over the internet but couldn't find the problem..
removing the ; after int main(void) didn't help
This is my code:
#include <stdio.h>
#include <cs50.h>
#include <math.h>
int main(void);
//Ask User for Height, and check
int a, b, rows, height;
int a = 0;
int b = 0;
int rows = 1;
do
{
printf ("Height: ");
height = GetInt();
}
while (height <=0 || height > 23);
//build half pyramid
do
{
do
{
printf("r");
a++;
}
while (a < height - rows);
do
{
printf("#");
b++;
}
while (b < rows + 1);
printf("\n");
rows++;
while (rows <= height);
}
I've been trying to solve this problem for a few days, but i just can't figure it out!
Thank you so much in advance!
int main(void);
You have just declared the main. You need to define it and add the code inside that definition.
int main()
{
.....
}
You got nested loop with do/while. Make sure that each start with do end with while.
Look like at the end of file, the "while" is not correct.
printf("\n");
rows++;
while (rows <= height);
}
That could be you missing the close '}' before 'while (rows <= height);'
Correct code could be:
int main(void)
{
//Ask User for Height, and check
int a, b, rows, height;
a = 0; // <- removed int
b = 0; // <- removed int
rows = 1; // <- removed int
do
{
printf ("Height: ");
height = GetInt();
}
while (height <=0 || height > 23);
//build half pyramid
do
{
do
{
printf("r");
a++;
}
while (a < height - rows);
do
{
printf("#");
b++;
}
while (b < rows + 1);
printf("\n");
rows++;
} // <- add }
while (rows <= height);
}
Main post is edited, so clear answer.
All your code is outside of a function because you're doing int main(); you're declaring a function. Use {} brackets instead.
int main() {
//Code here.
}
int a, b, rows, height;
int a = 0;
int b = 0;
Here in your above statements a and b are re-declared more than once causing compilation error.
Take the last while outside of the do scope:
while (rows <= height);
If you don't indent your code, which you (by all means) should do, at least write the starting and the ending curly brackets at once when you write the loop statement, before putting any code into that loop's body (which goes between the curly brackets). It will save you from troubles like these in the future.
In case of react native project .... its a simple issue.
You can go to you project target in ios and check Build, version and build identifier... they might have extra space on the end which should be removed
Hope it helps. Worked for me
Related
I am currently trying to print a pyramid of hashes for the Mario problem set (less comfortable), and they won't print. Would someone be able to look at my code and pinpoint where I am going wrong? Thank you so much in advance.
#include <cs50.h>
#include <stdio.h>
int main(void)
{
int height;
int hashes;
int space;
do
{
int height = get_int("height: ");
}
while (height < 0 || height > 5);
{
for (int i = 0; i < height; i++)
{
int hashes = i;
for (hashes = (i + 1); hashes >= height; hashes++)
{
printf("#");
}
}
}
}
Assume you are using CS50 Lab and make to compile. This program will not compile because of shadow variables (as mentioned in the comments by #Weather Vane), and other errors. Therefore, I assume the executable that's running (i.e. ./mario) was the last good compile of an older version of the source that did not print results.
The for loop in this code is an infinite loop. If you correct the compile errors, you will see nothing but #.
Recommend you delete the "old" compiled version (rm mario in the terminal) and then modify the code so 1) it compiles and 2) does not create an infinite loop.
Remember, you declare a variable including the type, as here: int height;. But when you use the variable (as here int height = get_int("height: ");) you do not use the type declaration.
try this code, hope it helps
#include<stdio.h>
#include<cs50.h>
int main(void){
int height ;
do
{printf("height: ");
height = get_int();
}
while(height<0 || height>23);
for(int i=1;i<height+1;i++)
{
for(int j=0;j<height-i;j++)
{
printf("%s"," ");
}
for(int l=0;l<1;l++)
{
printf("#");
}
for(int k=0;k<i;k++)
{
printf("#");
}
printf("\n");
}
}
Im trying to learn C, surely using the hard way and cant figure out this one error, could someone help? :-)
#include<stdio.h>
#include <stdlib.h>
#define max_X 15
#define max_Y 15
int x, y;
char Array[max_Y][max_X];
void displayArray(void){
for (y = 0; y < max_Y; y++) {
for (x = 0; x < max_X; x++) {
printf("%c",Array[y][x]);
}
printf("\n");
}
}
int main(void){
for (y = 0; y < max_Y; y++) {
for (x = 0; x < max_X; x++) {
Array[y][x] = '.';
}
}
displayArray;
getchar;
return(0);
}
Im trying to print out char array containing just dot characters using function. When i run it, there is just blank cmd and return value 0. I keep getting warnings about statements with no effect on these two lines:
displayArray;
getchar;
Can someone help? or give me a link to similar one where i can find answer to my problem? I was looking around but couldn't find anything i could compare to mine and understand at least a little.
You need to use parentheses even when the function you're using takes no arguments. So,
displayArray;
getchar;
should be:
displayArray();
getchar();
Also, return isn't a function. It's a keyword, so you can do:
return 0;
Use displayArray();
You should not call function like this displayArray;you can use this while providing address of function to function pointer.
So all I'm trying to do is take an input from the user of how many cards to use and then randomly assign each card to a different index in an array. I'm having extensive issues getting the rand function to work properly. I've done enough reading to find multiple different ways of shuffling elements in an array to find this one to be the easiest in regards to avoiding duplicates. I'm using GCC and after I input the amount of cards I never get the values from the array back and if I do they're all obscenely large numbers. Any help would be appreciated.
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
void main(){
srand(time(NULL));
int d, c, i, z, l, r;
printf("Enter the deck length: ");
scanf("%d\n ", &c);
int deck[c];
int swap[c];
z = c;
for(l=0; l<c; l++){
swap[l] = l;
}
for(i=z; i=0; i--){
r = rand() / i
deck[i] = swap[r];
for(r; r=(c-1); r++){
swap[r] = swap[(r+1)];
}
}
for(d = 0; d < c; d++){
printf("%d ", deck[d]);
}
return;
}
I can spot one major problem here:
for(i=z; i=0; i--)
^^^
This loop will never execute since you are using assignment(=) and setting i to 0 therefore the condition will always be false, although using equality(==) will still be false in this case, you probably want:
for(i=z; i!=0; i--)
This means you will be using deck unitialized which is undefined behavior. Once you fix that you have a similar problems here:
for(r; r=(c-1); r++){
main has to return int and your return at the end needs to provide a value.
Turning on warning should have allowed you to find most of these issues, for example using -Wall with gcc gives me the following warning for both for loops:
warning: suggest parentheses around assignment used as truth value [-Wparentheses]
Note, see How can I get random integers in a certain range? for guidelines on how to use rand properly.
You basically need to be able to generate 52 numbers pseudo-randomly, without repeating. Here is a way to do that...
First, loop a random number generator 52 times, with a method to ensure none of the random numbers repeat. Two functions in addition to the main() will help to do this:
#include <ansi_c.h>
int NotUsedRecently (int number);
int randomGenerator(int min, int max);
int main(void)
{
int i;
for(i=0;i<52;i++)
{
printf("Card %d :%d\n",i+1, randomGenerator(1, 52));
}
getchar();
return 0;
}
int randomGenerator(int min, int max)
{
int random=0, trying=0;
trying = 1;
while(trying)
{
srand(clock());
random = (rand()/32767.0)*(max+1);
((random >= min)&&(NotUsedRecently(random))) ? (trying = 0) : (trying = 1);
}
return random;
}
int NotUsedRecently (int number)
{
static int recent[1000];//make sure this index is at least > the number of values in array you are trying to fill
int i,j;
int notUsed = 1;
for(i=0;i<(sizeof(recent)/sizeof(recent[0]));i++) (number != recent[i]) ? (notUsed==notUsed) : (notUsed=0, i=(sizeof(recent)/sizeof(recent[0])));
if(notUsed)
{
for(j=(sizeof(recent)/sizeof(recent[0]));j>1;j--)
{
recent[j-1] = recent[j-2];
}
recent[j-1] = number;
}
return notUsed;
}
I'm writing a program in C to do a simple dynamic programming algorithm where you return the minimum number of coins needed to add up to a certain amount. Here's my code:
#include <limits.h>
#include <stdlib.h>
#include <stdio.h>
/*
This function returns the minimum number of stamps required for a given value.
It assumes that the given array contains the available stamp sizes, and that it
always contains 1, so a solution is always possible
*/
int min_number_of_stamps(const int* array, size_t array_size, int request) {
/* Construct a table with dimensions (array_size+1)*(request+1) */
int numRows = array_size + 1;
int numCols = request + 1;
int **DPtable;
DPtable = malloc(numRows*sizeof(int));
int i;
for (i = 0; i < numRows; i++) {
DPtable[i] = malloc(numCols*sizeof(int));
}
printf("%d",DPtable[4][0]);
int r, c, useIt, loseIt;
for (r = 0; r < numRows; r++) {
for (c = 0; c < numCols; c++) {
printf("%d,%d\n", r, c);
if (c==0) {
printf("1\n");
//if the amount of change is 0, 0 coins are needed
DPtable[r][c] = 0;
}
else if ((r==0) || c < array[r-1]) {
printf("2\n");
//if there are no coins or if the change needed is less than
//the smallest coin available, then 'infinity' coins are needed
DPtable[r][c] = INT_MAX;
}
else {
printf("3\n");
useIt = DPtable[r][c-array[r-1]] + 1;
loseIt = DPtable[r-1][c];
if (useIt <= loseIt) {
//if 'use it' requires fewer coins than 'lose it,' then
//'use it' coins are needed.
DPtable[r][c] = useIt;
}
else {
//if 'lose it' requires fewer coins, 'lose it' coins are needed
DPtable[r][c] = loseIt;
}
}
}
}
return DPtable[numRows][numCols];
}
int main() {
const int array[] = {1,5,10,25};
const int* stamps = &array[0];
printf("%d", min_number_of_stamps(stamps, 4, 44));
}
I'm getting a segfault when my inner for loop gets to the case where r=4 and c=0. I left my debugging print statements in because I'm lazy, but you can see where I got stuck. If I access the same place in the array outside of my for loops, there's no problem. But in the for loop, I get a `Segmentation fault: 11' message after it outputs "4,0" for the array element and "1" for the if case it's in. Can anyone see what I'm missing?
Learn to enable warnings & debugging for your compiler, i.e. gcc -g -Wall on Linux.
Learn to use a debugger, i.e. gdb -tui on Linux.
Consider using valgrind
EDIT
Many tutorials (in several languages, e.g. English, French, ....) for GCC, GDB, and ValGrind are easily found on the Web.
You're allocating dpTable incorrectly. It should be
DPtable = malloc(numRows*sizeof(int*));
See if that fixes the problem.
return DPtable[numRows][numCols];
thats out of bounds isn't it?
I have currently learning backtracking and got stuck on the 8-queen problem, I am using a 8x8 matrix and I think I've got some problems regarding the matrix passing to functions, any help would be highly apreciated.I wouldn't mind if anyone would bring any optimisation to the code, thanks.
here is my code.
#include <stdio.h>
#include <stdlib.h>
#define MAX 7
//void azzera(int **mat);
void posiziona(int **mat, int r,int c);
void stampa(int **mat);
int in_scacchi(int **mat,int r ,int c);
int main(int argc, char *argv[])
{
int i=0,j=0;
int **mat=(int **)malloc(sizeof(int *)*MAX);
for(i=0;i<=MAX;i++){
mat[i]=(int *)malloc(MAX*sizeof(int));
for(j=0;j<=MAX;j++){
mat[i][j]=-1;
}
}
printf("insert pos of the first queen on the first row (1-8) :");
scanf("%d",&i);
i-=1;
mat[0][i]=1;
posiziona(mat,1,0);
stampa(mat);
system("PAUSE");
return 0;
}
/*void azzera(int **mat){
int i=0,j=0;
for(i=0;i<=MAX;i++){
for(j=0;j<=MAX;j++){
mat[i][j]=-1;
}
}
}*/
void stampa(int **mat){
int i,j;
for(i=0;i<=MAX;i++){
for(j=0;j<=MAX;j++){
printf(" %d",mat[i][j]);
}
printf("\n");
}
}
void posiziona(int **mat, int r,int c){
int i=0,riga=1,flag_col=-1,flag_riga=-1;
if(riga<=7&&flag_riga!=1){
if(flag_riga==1){
flag_riga=-1;
posiziona(mat,r+1,0);
}
else if(in_scacchi(mat,r,c)==1){
if(c==MAX)
posiziona(mat,r-1,0);
posiziona(mat,r,c+1);
}
else{
flag_riga=1;
}
}
}
int in_scacchi(int **mat,int r ,int c){
int i,j,k,m;
int flag=0;
//col
for(i=0;i<r;i++){
for(j=0;j<=c;j++){
if(((mat[i][j]==1)&&(c==j)))
return 1;
}
}
//diag \
for(i=0;i<MAX-r;i++){
for(j=0;j<=MAX-c;j++){
if(mat[MAX-r-i][MAX-c-j]==1)
return 1;
}
}
//antidiag
for(i=r+1;i<=MAX;i++){
for(j=c+1;j<=MAX;j++){
if(mat[r-i][c+j]==1) {
return 1;
}
}
}
return 0;
}
1. One glaring problem is the memory allocation:
int **mat=(int **)malloc(sizeof(int *)*MAX);
for(i=0;i<=MAX;i++){
mat[i]=(int *)malloc(MAX*sizeof(int));
Given that MAX is 7, both mallocs are allocating too little memory for the matrix (seven elements instead of eight).
To be honest, I'd rename MAX to SIZE or something similar, and change all your loops to use strict less-than, i.e.
for(i = 0; i < SIZE; i++) {
I would argue that this is slightly more idiomatic and less prone to errors.
2. I haven't tried to debug the logic (I don't think it's fair to expect us to do that). However, I have noticed that nowhere except in main do you assign to elements of mat. To me this suggests that the code can't possibly be correct.
3. Beyond that, it may be useful to observe that in a valid solution every row of the chessboard contains exactly one queen. This means that you don't really need an 8x8 matrix to represent the solution: an 8-element array of column positions will do.
edit In response to your question in the comments, here is a complete Python implementation demonstrating point 3 above:
def can_place(col_positions, col):
row = len(col_positions)
for r, c in enumerate(col_positions):
if c == col or abs(c - col) == abs(r - row): return False
return True
def queens(n, col_positions = []):
if len(col_positions) >= n:
pretty_print(n, col_positions)
return True
for col in xrange(n):
if can_place(col_positions, col):
if queens(n, col_positions + [col]):
return True
return False
def pretty_print(n, col_positions):
for col in col_positions:
print '.' * col + 'X' + '.' * (n - 1 - col)
queens(8)
Your matrix must iterate from 0 to MAX-1,
i.e
int **mat= malloc(sizeof(int *)*MAX);
for(i=0;i< MAX;i++){ //see for i<MAX
mat[i]= malloc(MAX*sizeof(int));
for(j=0;j<MAX;j++){ //see for j<MAX
mat[i][j]=-1;
}
}
malloc must be called with sizeof(...) * (MAX+1) in both the i- and j-loop.
Moreover, when I ran your program I got an access violation in the antidiag portion of in_scacchi(...) due to the fact that the code tries to access mat[r-i][c+j] which evaluates to mat[-1][1] because r==1 and i==2.
So there seems to be a logical error in your description of the anti-diagonal of the matrix.