Hello i am new here i liked this community:
I have a c++ homework could you check it if it is correct?
1. Rewrite the following loop so it uses pointer notation (with the indirection operator) instead of the subscript notation:
for (int x = 0; x < 100; x++)
cout<<arr[x]<<endl;
Answer:
for (int indoperator = 0; indoperator < 100; indoperator ++) {
cout << "*(x + " << indoperator << ") = " << *(x + indoperator) << endl;
2. Assume ptr is a pointer to an int, and holds the address 12000. On a system with 4-byte integers, what address will be in ptr after the following statement?
ptr += 10;
Answer:
ptr += 10 means we add 4 bytes 10 times so we add +40 to 12000
12040 will be the address
3. Consider the following strings (array of characters):
char s1[13] = “Hello”;
char s2[7] = “World!”;
a. Write a code that appends (concatenates) s2 to s1.
b. Print the string s1.
Answer:
strcat (s1, s2);
cout<<"The string becomes: "<< s1 ;
4. Consider the following strings:
char s1[20];
char * s2 = “Welcome to Paris!”;
char * s3 = “London”;
a. Using a string function, copy 11 characters from s2 to s1. Make sure the null character terminates s1.
b. Using a string function, append s3 to s1.
c. Print s1.
Answer:
strncpy (s2, s1, 11);
s1[11] = '\0' ;
strcat (s3, s1);
cout<<s1;
I have one more question could you give me some hints:
1. Write a program that creates a 4 x 5 two-dimensional array initialized with integer values. Call the following functions:
(int array[4][5])
a. getAverage: a function that:
i. Takes as arguments a 2-dimensional array with its sizes
ii. Returns a double: the average of all the values in the array
getAverage(array[].4.5)
{
(for int i=0 . int j=0 , i<4 && j<5, i++,j++)
{
return (array[i]+array[j])/20)
b. getRowTotal: a function that:
i. Takes as arguments a 2-dimensional array with its sizes, and an integer argument that specifies a specific row subscript that you pass from the main function
ii. Returns the total of the values in the specified row
(i dont know how to get the row only)
d. Print all the results returned by the functions.
how do we print everything?
Thanks for you help i appreciate it a lot. The homework is due today after 12 hours but its okay if i dont get an answer today i just want to know how to do it for my knowledge not for the grade. Thank you