Question

In: Computer Science

I'm having trouble getting a certain output with this code #include <stdio.h> //function prototypes void initializeArray(int...

I'm having trouble getting a certain output with this code

#include <stdio.h>

//function prototypes
void initializeArray(int size, int ids[]);
void printArray(int size, int * idPointer);


int main(void) {
// 1. declare an array of 5 integers called ids
int ids[5];
  
// 2. declare an integer pointer called arrayPointer and
// initialize it to point to the array called ids
int *arrayPointer = ids;


// 3. call initializeArray() function sending to it
// 5 for the size and the array called ids
initializeArray(5,ids);
  
  
// 4. add 3 to the value at where arrayPointer is pointing to
*(arrayPointer) += 3;


// 5. add 5 to the value at 2 locations past
// where arrayPointer is pointing to
*(arrayPointer+1) += 5;
  
  
// 6. call printArray() function sending to it
// 5 for the size and arrayPointer
printArray(5, arrayPointer);
  
  
return 0;
}


// This function initializes an array ids of size "size"
void initializeArray(int size, int ids[]) {
    int i;
    for (i = 0; i < size; i++) {
        ids[i] = i * 100;
    }
}


// This function prints an array of size "size". The array is pointed at by idPointer
void printArray(int size, int * idPointer) {
    int i;
    for (i = 0; i < size; i++) {
        // 7. finish the code for the printf() statement
        printf("Element at index %d is %d\n", i,*(idPointer+i));
    }
}
Element at index 0 is 3
Element at index 1 is 100
Element at index 2 is 205
Element at index 3 is 300
Element at index 4 is 400

Solutions

Expert Solution

#include <stdio.h>

//function prototypes
void initializeArray(int size, int ids[]);

void printArray(int size, int *idPointer);


int main(void) {
// 1. declare an array of 5 integers called ids
    int ids[5];

// 2. declare an integer pointer called arrayPointer and
// initialize it to point to the array called ids
    int *arrayPointer = ids;


// 3. call initializeArray() function sending to it
// 5 for the size and the array called ids
    initializeArray(5, ids);


// 4. add 3 to the value at where arrayPointer is pointing to
    *(arrayPointer) += 3;


// 5. add 5 to the value at 2 locations past
// where arrayPointer is pointing to
    *(arrayPointer + 2) += 5;


// 6. call printArray() function sending to it
// 5 for the size and arrayPointer
    printArray(5, arrayPointer);
    return 0;
}


// This function initializes an array ids of size "size"
void initializeArray(int size, int ids[]) {
    int i;
    for (i = 0; i < size; i++) {
        ids[i] = i * 100;
    }
}


// This function prints an array of size "size". The array is pointed at by idPointer
void printArray(int size, int *idPointer) {
    int i;
    for (i = 0; i < size; i++) {
        // 7. finish the code for the printf() statement
        printf("Element at index %d is %d\n", i, *(idPointer + i));
    }
}


Related Solutions

I'm having trouble determining the lines of code for 4-6 #include <stdio.h> //function prototypes void initializeArray(int...
I'm having trouble determining the lines of code for 4-6 #include <stdio.h> //function prototypes void initializeArray(int size, int ids[]); void printArray(int size, int * idPointer); int main(void) { // 1. declare an array of 5 integers called ids int ids[1,2,3,4,5];    // 2. declare an integer pointer called arrayPointer and // initialize it to point to the array called ids int *arrayPointer = ids; // 3. call initializeArray() function sending to it // 5 for the size and the array...
What’s the output of following code fragment: #include<stdio.h> #include<signal.h> void response (int sig_no) { printf("25"); }...
What’s the output of following code fragment: #include<stdio.h> #include<signal.h> void response (int sig_no) { printf("25"); } void response2 (int sig_no) { printf("43"); }     int main() {      int id = getpid();      signal(SIGUSR1, response); signal(SIGKILL, response2); for(int i=0; i<4; i++) { sleep(1); if (i % 3 == 0) { kill(id, SIGUSR1); } else { kill(id, SIGKILL); } } return 0; }
What is the output of the following C program? #include<stdio.h> int fac (int x); void main(...
What is the output of the following C program? #include<stdio.h> int fac (int x); void main( ) {                         for (int i=1; i<=2; i++)                                     printf("%d", fac(i)); } int fac(int x) {                         x = (x>1) ? x + fac(x-1) : 100);                         return x; }
example_thread.c #include <stdio.h> #include <stdlib.h> #include <pthread.h> int shared= 0; void race(void); int main(){     pthread_t...
example_thread.c #include <stdio.h> #include <stdlib.h> #include <pthread.h> int shared= 0; void race(void); int main(){     pthread_t player1, player2, player3;     pthread_create(&player1, NULL, (void *)race, NULL);     pthread_create(&player2, NULL, (void *)race, NULL);     pthread_create(&player3, NULL, (void *)race, NULL);     pthread_join(player1, NULL);     pthread_join(player2, NULL);     pthread_join(player3, NULL);     printf("Total Number = %d\n", shared);     return 0; } void race(void) {     long i,tmp;     for(i=1; i<=200000; i++) {         tmp = shared;         tmp = tmp + 1;         shared =...
#include <stdio.h> #include <math.h> int fun(int); int main(void)    {     int i = 5, x...
#include <stdio.h> #include <math.h> int fun(int); int main(void)    {     int i = 5, x = 3;     i = fun(x);     printf("%d\n", i);     return 0; } int fun(int i) {      int res = 0;      res = pow (i , 3.0);      return ( res); }
please fix code #include <stdio.h> #include <stdlib.h> #include <string.h> // function declarations int getValidJerseyNumber(); int getValidRating();...
please fix code #include <stdio.h> #include <stdlib.h> #include <string.h> // function declarations int getValidJerseyNumber(); int getValidRating(); int main() { // declaring variables int size = 5; int jerseyNo[size]; int rating[size]; int i = 0, jno, rate; char option; /* Getting the inputs entered by the user * and populate the values into arrays */ for (i = 0; i < size; i++) { printf("Enter player %d's jersey number:", i + 1); jerseyNo[i] = getValidJerseyNumber(); printf("Enter player %d's rating:\n", i +...
my code is not printing the output #include <stdio.h> #include<stdbool.h> int main(){ // variable declarations   bool...
my code is not printing the output #include <stdio.h> #include<stdbool.h> int main(){ // variable declarations   bool binary[12] = {false,false, false, false,false,false,false,false,false,false,false, false};   int powerTwo[12] = {2048.1028,576, 256, 128, 64, 32, 16, 8 , 4, 2, 1};   int oneZero[9]= {0,0,0,0,0,0,0,0,0};   int tempVal = 0;   double decimal = 0;   int i = 0;   int j = 0;   // Intialization   printf("Starting the CPSC 1011 Decimal to Binary Converter!\n");   printf("Please enter a positive whole number (or EOF to quit): \n");   scanf("%lf", &decimal);   printf("%lf", decimal);...
Given a program as shown below: #include <stdio.h> void function1(void); void function2 (int, double x); void...
Given a program as shown below: #include <stdio.h> void function1(void); void function2 (int, double x); void main (void) { int m; double y; m=15; y=308.24; printf ("The value of m in main is m=%d\n\n",m); function1(); function2(m,y); printf ("The value of m is main still m = %d\n",m); } void function1(void) { printf("function1 is a void function that does not receive\n\\r values from main.\n\n"); } void function2(int n, double x) { int k,m; double z; k=2*n+2; m=5*n+37; z=4.0*x-58.4; printf ("function2 is...
Given a program as shown below: #include <stdio.h> void function1(void); void function2 (int, double x); void...
Given a program as shown below: #include <stdio.h> void function1(void); void function2 (int, double x); void main (void) { int m; double y; m=15; y=308.24; printf ("The value of m in main is m=%d\n\n",m); function1(); function2(m,y); printf ("The value of m is main still m = %d\n",m); } void function1(void) { printf("function1 is a void function that does not receive\n\\r values from main.\n\n"); } void function2(int n, double x) { int k,m; double z; k=2*n+2; m=5*n+37; z=4.0*x-58.4; printf ("function2 is...
#include <stdio.h> int main(void) { float funds = 1.0, cost = 0.1; int candies = 0;...
#include <stdio.h> int main(void) { float funds = 1.0, cost = 0.1; int candies = 0; while(cost <= funds) { candies += 1; funds -= cost; cost += 0.1; } printf("%d candies with $%.2f left over.\n",candies,funds); return 0; } When you compile and run this code you get 3 candies with $0.40 left over. Without knowing how floating point numbers work in a computer, would this result be expected? Explain why or why not. Explain why this result, in fact,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT