1. The following function uses reference variables as parameters. Rewrite the fu
ID: 3547138 • Letter: 1
Question
1.
The following function uses reference variables as parameters. Rewrite the function so it uses pointers instead of reference variables, and then demonstrate the function in a complete program.
int dosomething(int &x, int &y)
{
int temp =x;
x = y * 10;
y = temp * 10;
return x + y;
}
2.
Write a function that accepts an int array and the array and the arrays size as arguments. The function should create a new array that is one element larger than the argument array. The first element of the new array should be set to 0. Element 0 of the argument array should be copied to element 1 of the new array, element 1 of the argument array should be copied to element 2 of the new array, and so forth. The function should return a pointer to the new array.
Explanation / Answer
2.
#include<stdlib.h>
#include<iostream.h>
#include<string.h>
char *a(char *,int);
int main(char *argv[],int argc)
{
int x,n,i=0,t,num=0;
FILE *fd;
char *grid;
char *y;
grid = (char *)malloc(sizeof(char) * 9 * 9);
y=(char *)malloc(sizeof(char) * 9 * 9);
cout<<"enter value";
cin>>x;
if(x<50)
{
n=x;
if (argv[1] != NULL){
fd = fopen(argv[1], "r");
while (num!= n)
{
fscanf(fd, "%c",grid[i]);
num++;
i++;
}
}
y[n+1]=a(grid,n);
for(i=0;i<n;i++)
cout<y[i]<<endln;
return(0);
}
else if(x>50)
return 0;
}
char *a(char *grid[],int n)
{
char x[n+1];
int i;
x[0]=0;
for(i=0;i<n;i++)
x[i+1]=grid[i];
return x;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.