anyone possible to answer of these question? i did some but not all and not sure
ID: 3763768 • Letter: A
Question
anyone possible to answer of these question? i did some but not all and not sure it is right. also i dont understand pointers and * and & these.
1 .Question: write a C function which takes two strings and returns 1
if they are identical and zero otherwise.
2. Question : What is the output of the program below? Show your work,
not just the answer it doesn’t have to be elaborate, but I want to
see your reasoning.
code :
#include <stdio.h>
int main(){
int arr[] = {2,3,4}
int a
int *p1
int *p2
int *p3
a = arr[0]
p1 = &(arr[0])
p2 = p1
p3 = &a
a++
p1++
(*p2)++
(*p3)++
printf(“a : %i ”, a)
printf(“*p1 : %i ”, *p1)
printf(“*p2 : %i ”, *p2)
printf(“*p3 : %i ”, *p3)
return 0
}
answer :
3 Question: the code below does not compile. Why not?
code :
typedef struct {
int a
int b
} THING
int main(){
THING.a = 1
THING.b = 2
return 0
}
4 Question : You are given an array of n integers representing
personality test scores used by a human resources department. The
scores are categorized as:
80 or above: YES!
60-79:MAYBE
59 and below: NOOO!
Write a C function which takes such an array, the integer n and
reports the number of entries in each category via three additional
parameters.
It is up to you to determine the precise types of the parameters.
Also give a code segment as an examnple of how to invoke your
function.
Hint: remember how we implemented a swap function?
code : ?
5 Question : Write a function extract_diag described as follows:
It takes one parameter: an DIM by DIM 2D array of integers where DIM is a constant (established via #define).
It dynamically allocates an int array of length DIM.
Populates the allocated array with the diagonal elements of the 2D array from upperleft to lowerright.
(Note that since thematrix is square, the diagonal is welldefined)
returns the newly allocated array.
Hint: malloc
code : ?
Question 6 (18 points): Below is a typedef for of a type called STUFF
followed by three variable declarations.
This is followed by a table of expressions involving these variables.
For each expression select one of I..V which best describes the type
of the expression.
typedef struct {
int a
int b
char name[10]
}STUFF
// variable declarations
STUFF x
STUFF *p
STUFF things[100]
type A - H
A.int
B.pointer to int
C.STUFF
D.Pointer to STUFF
E.Pointer to Pointer to STUFF
F.char
G.pointer to char
H.Invalid Expression
these are Expression and choose from type a - h
things[20]
x->a
*p
things[20]->a
x.a
&(x.name[2])
(*p)->b
&x
&(things[12])
*(a[3])
&p
&(things[10].a)
7 Question : Write a C function which takes a DIMxDIM integer array
(for some constant DIM) and labels the entries 0..DIM 2 1
as follows:
The upper left entry (row0,col0) is 0.
Subsequent numbers are assigned along SWtoNE
diagonals,
layerbylayer.
For example, if DIM=4, your function would result in the following
assignment to entries in the array:
0 2 5 9
1 4 8 12
3 7 11 14
6 10 13 15
(the individual “layers” are shown by alternating shading).
code :
Question : Below is an attempt to write a “swap” function. It
doesn’t work correctly.
(a) Write a correct version and
(b) Write a code segment showing how to invoke it correctly
void swap_wrong(int a, int b) {
int tmp
tmp = a
a = b
b = tmp
} this is wrong
correction ?
Explanation / Answer
#include <stdio.h>
#include <conio.h>
#include <string.h>
int isAnagram(char *firstString, char *secondString);
int main(){
char firstString[100], secondArray[100];
printf("Enter first String ");
gets(firstString);
printf("Enter second String ");
gets(secondArray);
if(isAnagram(firstString, secondArray) == 1){
printf("%s and %s are Anagram ",firstString,secondArray);
} else {
printf("%s and %s are not Anagram ",firstString,secondArray);
}
getch();
return 0;
}
/*
* Function to check whether two passed strings are anagram or not
* returns 1 if anagram otherwise 0
*/
int isAnagram(char *firstString, char *secondString){
int firstCharCounter[256] = {0}, secondCharCounter[256] = {0};
int counter;
/* Two Strings cannot be anagram if their length is not equal */
if(strlen(firstString) != strlen(secondString)){
return 0;
}
/* count frequency of characters of firstString */
for(counter = 0; firstString[counter] != ''; counter++){
firstCharCounter[firstString[counter]]++;
}
/* count frequency of characters of secondString */
for(counter = 0; secondString[counter] != ''; counter++){
secondCharCounter[secondString[counter]]++;
}
/* compare character counts of both strings,
If not equal return 0, otherwise 1 */
for(counter = 0; counter < 256; counter++){
if(firstCharCounter[counter] != secondCharCounter[counter])
return 0;
}
return 1;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.