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

Nick Evan commented: No code-tags, but gotta +rep the attitude +12

getAverage(array[].4.5) is not correct. When passing in a 2D array only the last dimension is needed, so it would be getAverage(int array[][5]) in the function definition.
Make life easier on yourself and use a nested for loop approach to the averaging function:

for (int i = 0;  etc        )
     for(int j = 0; etc)
              add to running total array[i][j]

So hint, for the function using one row of that you'd still pass in the array the same as the average function but loop through holding i constant (set i to something and then run through the j's values).

The others looked ok but that doesn't mean I didn't miss something.

ah ok thanks jonsca. So i set i to 4 for example and loop j and put the answer in a variable. thank you !

Yup, you got it. No problem!

There is something wrong with an answer to the problem 4. Hint: an answer to the problem 3 is correct.

commented: Yup. I glazed over it! +2
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.