I have an assignment where we've been asked to analyze the code below and fill in what the printf staments should be. I'm kind of stuck, so any recommendations would be greatly appreciated.
#include <stdio.h>
#include <stdlib.h>
int maximum(int x, int y)
{
if(x > y)
{
return x;
}
if(x < y)
{
return y;
}
return 0;
}
int main( int argc, char *argv[] )
{
int first;
int second;
int data;
if( argc != 3) {
printf("
retunr 0;
}
first = atoi(argv[1]);
second = atoi(argv[2]);
data = maximum(first,second);
if(data)
{
printf("
}
else
{
printf("
}
return 0;
}
If you want to print data value
printf("%d", data);
If you want to print any random string value
printf("Print whatever you want here");
Related
struct Student
{
char* name;
int balls;
};
void inputdata(Student **s, int *n)
{
int nn;
printf("%s\n", "Input amount of students");
scanf("%i", &nn);
Student* a = new Student[nn];
for (int i = 0; i < nn; ++i)
{
scanf("%s", &a[i].name);
scanf("%i", &a[i].balls);
}
n = &nn;
s = &a;
}
void print(Student **s, int n)
{
for (int i = 0; i < n; ++i)
{
printf("%s %i\n", s[i]->name, s[i]->balls);
}
}
int main(int argc, char const *argv[])
{
Student** s;
int *n;
inputdata(s, n);
print(s, *n);
return 0;
}
So how am I supposed to input data and print data on console screen.
I kinda input data, ok, unable to print it on my screen. Program ends. What am I supposed to fix here?
You should pass pointers to what should be modified in callee functions.
Callee functions should dereference pointers passed to modify what should be modified.
You have to allocate for strings before reading.
It is inconsistent that an array of Student in the function inputdata but an array of Student* is required in the function print.
You should limit the maximum length to read to the number of elements in the buffer minus one to prevent buffer overrun when you use %s. The "minus one" is for terminating null-character.
Fixed code:
#include <cstdio>
struct Student
{
char* name;
int balls;
};
void inputdata(Student **s, int *n)
{
int nn;
printf("%s\n", "Input amount of students");
scanf("%i", &nn);
Student* a = new Student[nn];
for (int i = 0; i < nn; ++i)
{
a[i].name = new char[4096];
scanf("%4095s", a[i].name);
scanf("%i", &a[i].balls);
}
*n = nn;
*s = a;
}
void print(Student *s, int n)
{
for (int i = 0; i < n; ++i)
{
printf("%s %i\n", s[i].name, s[i].balls);
}
}
int main(int argc, char const *argv[])
{
Student* s;
int n;
inputdata(&s, &n);
print(s, n);
return 0;
}
I was solving a competitive programming question on codeforces. My code got accepted there but it is giving segmentation fault in my local computer. Why is it so?
I also tried other online compilers like ideone , and it was working there too.
My operating system is Ubuntu 20.04
My code :
#include <bits/stdc++.h>
using namespace std;
int M = 1000000007;
int val[1001][1001];
int n,k;
int dp(int cur,int rem)
{
if(cur<1 || cur>k || rem<0 || rem>n)return 0;
if(cur==1 || rem==0)return 1;
if(val[cur][rem]==-1)
{
int ans=0;
ans+=dp(cur,rem-1);
ans%=M;
ans+=dp(cur-1,n-rem);
ans%=M;
val[cur][rem]=ans;
}
return val[cur][rem];
}
void solve()
{
cin>>n>>k;
for(int i=0;i<=k;i++)for(int j=0;j<=n;j++)val[i][j]=-1;
cout<<dp(k,n);
cout<<"\n";
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int _t=1;
cin>>_t;
for (int i=1;i<=_t;i++)
{
solve();
}
return 0;
}
Turns out, my stack size was less. I used this stackoverflow answer to modify my code. Here is the correct code :
#include <sys/resource.h>
#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
int M = 1000000007;
int val[1001][1001];
int n,k;
int dp(int cur,int rem)
{
if(cur<1 || cur>k || rem<0 || rem>n)return 0;
if(cur==1 || rem==0)return 1;
if(val[cur][rem]==-1)
{
int ans=0;
ans+=dp(cur,rem-1);
ans%=M;
ans+=dp(cur-1,n-rem);
ans%=M;
val[cur][rem]=ans;
}
return val[cur][rem];
}
void solve()
{
cin>>n>>k;
for(int i=0;i<=k;i++)for(int j=0;j<=n;j++)val[i][j]=-1;
cout<<dp(k,n);
cout<<"\n";
}
signed main()
{
const rlim_t kStackSize = 64L * 1024L * 1024L; // min stack size = 64 Mb
struct rlimit rl;
int result;
result = getrlimit(RLIMIT_STACK, &rl);
if (result == 0)
{
if (rl.rlim_cur < kStackSize)
{
rl.rlim_cur = kStackSize;
result = setrlimit(RLIMIT_STACK, &rl);
if (result != 0)
{
fprintf(stderr, "setrlimit returned result = %d\n", result);
}
}
}
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int _t=1;
cin>>_t;
for (int i=1;i<=_t;i++)
{
solve();
}
return 0;
}
I have to make a program with a hash table that maps single random characters into the table. The program kind of works but sometimes it crashes, also it doesn't map every element. Some of them just won't get inside the table and there are always spare spaces in the table. I don't know what to do to solve these 2 problems. I used 3 versions of open adressing and each of them causes the same 2 problems. Sorry for my bad English. Thank you in advance.
Edited. Of course, I forgot about dynamic allocation. But the problem isn't solved.
#include <time.h>
#include <string>
#include <cstdlib>
using namespace std;
int Liniowo(int i, int proby, int rozmiar) // (open adressing, Linear probing)
{
if(i+proby<rozmiar)
return i+proby;
else
{
return -1;
}
}
int Kwadratowo(int i, int proby, int rozmiar) // (open adressing, Quadratic probing)
{
if (i+proby*proby<rozmiar)
return i+proby*proby;
else
{
return -1;
}
}
int Podwojnie(int i, int proby, int rozmiar, char klucz) // (open adressing, Double hashing)
{
if (i*(klucz*(701%klucz)-klucz%13)<rozmiar&&i*(klucz*(701%klucz)-klucz%13)>0)
return i*(klucz*(701%klucz)-klucz%13);
else
{
return -1;
}
}
int modularnie(char c,int rozmiar) // modular
{
return c%rozmiar;
}
void dodaj(char *tab,int max, char c) // add an element
{
int i=modularnie(c, max);
if (tab[i]== '\0')
tab[i]=c;
else
{
int u=0;
int h;
while (tab[i]!= '\0'&&h!=-1)
{
u++;
// h=Kwadratowo(i, u, max);
h=Podwojnie(i,u,max,c);
}
if (h!=-1)
tab[h]=c;
else
cout << "no niestety, nie udalo sie wstawic " <<endl; //"I couldn't map the element"
}
}
int wyszukaj(char *tab,int max, char c) // search an element
{
int i=modularnie(c, max);
int j=i;
if (tab[i]== '\0')
return -1;
while (tab[i]==c)
{
i=(i+1)%max;
if((i==j)||(tab[i]== '\0'))
return -1;
}
return i;
}
int usun(char *tab,int max, char c) // remove an element
{
int r,j,i=wyszukaj(tab,max,c);
j=i;
if (i==-1)
return -1;
tab[i]= '\0';
while (tab[(++i)%max]!= '\0')
{
i%=max;
r=modularnie(tab[i],max);
if (((i<r)&&(r<=j)) || ((r<=j)&&(j<i)) || ((j<i)&&(i<r)))
{
tab[j]=tab[i];
tab[i]= '\0';
j=i;
continue;
}
}
return 0;
}
int main()
{
srand( time( NULL ) );
int ile;
cout << "podaj wielkosc tablicy: "; //"Type the size of the table"
cin >> ile;
char* tab; // EDITED
tab=new char(ile);
for (int n=0; n<ile; n++)
{
tab[n]= '\0';
}
char e;
for (int i=0; i<ile; i++)
{
e='!'+rand()%127;
dodaj(tab, ile, e);
}
for(int j=0; j<ile; j++)
{
cout << j << ", " << tab[j] << endl;
}
return 0;
}
I 'm reading "The Algorithm Design Manual (2nd Edition)". C++ is new for me.
I try to use example of author: string_compare(), and only code by myself main(). Output is wrong. I guess my main 's having problem with char s[], pointer.
Anyone can help me finding my mistake.
Code by C++, and very simple input
int main()
{
char s[] = "A"; // "thou shalt not"; //"FOOD";
char t[] = "B"; // "you should not"; //"MONEY";
int i = sizeof(s)/sizeof(char);
int j = sizeof(t)/sizeof(char);
int resultDistance = string_compare(s, t, i, j);
printf("N steps = %d\n", resultDistance);
reconstruct_path(s, t, i, j);
}
int string_compare(char *s, char *t, int i, int j)
{
int k; /* counter */
int opt[3]; /* cost of the three options */
int lowest_cost; /* lowest cost */
if (i == 0) return(j * indel(' '));
if (j == 0) return(i * indel(' '));
opt[MATCH] = string_compare(s,t,i-1,j-1) + match(s[i],t[j]);
opt[INSERT] = string_compare(s,t,i,j-1) + indel(t[j]);
opt[DELETE] = string_compare(s,t,i-1,j) + indel(s[i]);
lowest_cost = opt[MATCH];
for (k=INSERT; k<=DELETE; k++)
if (opt[k] < lowest_cost) lowest_cost = opt[k];
m[i][j].cost = lowest_cost; /* REMOVE FROM PRINTED VERSION */
return( lowest_cost );
}
int reconstruct_path(char *s, char *t, int i, int j)
{
/*printf("trace (%d,%d)\n",i,j);*/
if (m[i][j].parent == -1) return(0);
if (m[i][j].parent == MATCH) {
reconstruct_path(s,t,i-1,j-1);
match_out(s, t, i, j);
return(0);
}
if (m[i][j].parent == INSERT) {
reconstruct_path(s,t,i,j-1);
insert_out(t,j);
return(0);
}
if (m[i][j].parent == DELETE) {
reconstruct_path(s,t,i-1,j);
delete_out(s,i);
return(0);
}
}
int match_out(char *s, char *t, int i, int j)
{
if (s[i]==t[j]) printf("M");
else printf("S");
return(0);
}
void insert_out(char *t, int j)
{
printf("I");
}
void delete_out(char *s, int i)
{
printf("D");
}
int indel(char c)
{
return(1);
}
int match(char c, char d)
{
if (c == d) return(0);
else return(1);
}
My code on github: https://github.com/hoangvu1991/EditDistanceRecursive/blob/master/EditDistanceRecursive.cpp
actual: 0 | expect:1
Try following:
opt[MATCH] = string_compare(s,t,i-1,j-1) + match(s[i-1],t[j-1]);
instead of
opt[MATCH] = string_compare(s,t,i-1,j-1) + match(s[i],t[j]);
why my program Time Limited Error?
because of the sort?
this is the question
link text
#include <cstdio>
#include <cstring>
using namespace std;
int map[22][44];
int book[22];
int total;
int sum;
int way[22];
int tails[22];
int tail;
void init()
{
memset(map,0,sizeof(map));
memset(book,0,sizeof(book));
sum =0;
memset(way,0,sizeof(way));
way[1]=1;
memset(tails,0,sizeof(tails));
}
void sort()
{
int t;
for (int i=1;i<=22;i++)
{
if (tails[i]==0)
break;
else
{
for (int j=1;j<=tails[i]-1;j++)
for (int k=j+1;k<=tails[i];k++)
{
if (map[i][j] > map[i][k])
{
t = map[i][j];
map[i][j]=map[i][k];
map[i][k]=t;
}
}
}
}
}
void dfs(int x,int y)
{
if ((x < 1)||(x > 22))
return;
if (book[x]==1)
return;
//printf("%d \n",x);
if (x == total)
{
sum++;
for (int i=1;i<=y-1;i++)
{
printf("%d ",way[i]);
}
printf("%d",total);
printf("\n");
return;
}
tail = tails[x];
for (int i=1;i<=43;i++)
{
book[x]=1;
way[y]=x;
dfs(map[x][i],y+1);
book[x]=0;
}
}
int main()
{
int temp1,temp2;
//freopen("ex.in","r",stdin);
//freopen("ex.out","w",stdout);
int c = 0;
while(scanf("%d",&total)!=EOF)
{
c++;
printf("CASE ");
printf("%d",c);
printf(":");
printf("\n");
init();
for (;;)
{
scanf("%d%d",&temp1,&temp2);
if ((temp1 == 0)&&(temp2 == 0))
break;
else
{
tails[temp1]++;
tail = tails[temp1];
map[temp1][tail]=temp2;
tails[temp2]++;
tail = tails[temp2];
map[temp2][tail]=temp1;
}
}
sort();
dfs(1,1);
printf("There are ");printf("%d",sum);printf(" routes from the firestation to streetcorner ");printf("%d",total);printf(".");
printf("\n");
}
return 0;
}
Because your sorting algoritm is in worst-case O(n*n), you can use InnoSort for better worst-case complexity O(n*log(n)).
You are using C++ then use sort function from <algorithm> header to do this simplest.
Documentation you can find at http://www.sgi.com/tech/stl/sort.html
For a start, you're accessing tails and map past the end. C++ arrays are zero-indexed, so the first element is 0, and the last valid elements are tails[21] and map[21][43].