Question

In: Computer Science

C PROGRAMMING: SHIFT TYPEDEFS INSIDE OF AN ARRAY. I have an array of NODES, NODE nodes[513];...

C PROGRAMMING: SHIFT TYPEDEFS INSIDE OF AN ARRAY.

I have an array of NODES,

NODE nodes[513];

typedef struct node {
int weight;   
} NODE;

I have a value called int num_nodes which gives the number of positions in that array which are filled.

What I need to do:

there is a value inside of nodes[0], but there are other values in the node array which can be found at index nodes[256] to nodes[256+num_nodes]. I need to shift all of these nodes that are in the far part of the array so that they appear after node[0]. the nodes which are unused have their weight set to -1. i need to shift till there are no -1 between root and the used nodes

How?

Also, I am NOT ALLOWED TO USE ARRAY BRACKETS [ ] ONLY POINTERS to navigate the array.

Solutions

Expert Solution

//C program

#include<stdio.h>

typedef struct node {
int weight;   
} NODE;
NODE nodes[513];

int main(){
   int i;
   (nodes+0)->weight = 100;
   int num_nodes = 20;;
   for(i=1;i<513;i++)(nodes+i)->weight = -1;
   for(i =256;i<256+num_nodes;i++)(nodes+i)->weight = i+num_nodes;
  
   printf("array before shifting\n ");
  
   for(i=0;i<513;i++){
       printf("%d ",(nodes+i)->weight);
       if(i!=0&&i%10==0)printf("\n");
   }
   for(i=1;i<=num_nodes;i++){
       (nodes+i)->weight = (nodes+255+i)->weight;
       (nodes+255+i)->weight= -1;
   }
   printf("\n\narray after shifting\n ");
  
   for(i=0;i<513;i++){
       printf("%d ",(nodes+i)->weight);
       if(i!=0&&i%10==0)printf("\n");
   }
   return 0;
}


Related Solutions

I need C++ programming with output. I have tried other programming and it does not work....
I need C++ programming with output. I have tried other programming and it does not work. So please give me the one that actually works. Assignment 1 Design your own linked list class that works as a template class. It should provide member functions for appending, inserting and deleting nodes. The destructor should destroy the list. The class should also provide a member function that will display the contents of the list to the screen. The class should also provide...
Write a binary search tree with 10 nodes in Java, each node will have 3 attributes...
Write a binary search tree with 10 nodes in Java, each node will have 3 attributes (data, x, y). The binary tree need to have function "add()" to add new node into the tree. After added all 10 nodes, it will be sorted and turn into a balanced binary search tree.
Using C programming I have a file that contains earthquake data that I will copy and...
Using C programming I have a file that contains earthquake data that I will copy and paste below. I want to use either bubble or insertion sort to sort the file by latitude in ascending order, then create a new file containing the sorted data. example file to sort: time,latitude,longitude,depth,mag,magType,nst,gap,dmin,rms,net 2020-10-17T17:22:03.840Z,32.877,-116.2991667,0.31,1.16,ml,21,119,0.07747,0.26,ci 2020-10-17T17:17:29.980Z,34.1611667,-116.452,2.75,0.87,ml,17,66,0.05224,0.22,ci 2020-10-17T17:03:54.460Z,33.5396667,-116.4613333,8.66,0.63,ml,18,126,0.06084,0.16,ci 2020-10-17T16:55:01.080Z,63.254,-151.5232,8,1.4,ml,,,,0.9,ak
Programming in C++ Write a program that prints the values in an array and the addresses...
Programming in C++ Write a program that prints the values in an array and the addresses of the array’s elements using four different techniques, as follows: Array index notation using array name Pointer/offset notation using array name Array index notation using a pointer Pointer/offset notation using a pointer Learning Objectives In this assignment, you will: Use functions with array and pointer arguments Use indexing and offset notations to access arrays Requirements Your code must use these eight functions, using these...
The purpose of this C++ programming assignment is to practice using an array. This problem is...
The purpose of this C++ programming assignment is to practice using an array. This problem is selected from the online contest problem archive, which is used mostly by college students worldwide to challenge their programming ability and to prepare themselves for attending programming contests such as the prestige ACM International Collegiate Programming Contest. For your convenience, I copied the description of the problem below with my note on the I/O and a sample executable. Background The world-known gangster Vito Deadstone...
c++ Redo Programming Exercise 14 by first sorting the array before determining the array elements that...
c++ Redo Programming Exercise 14 by first sorting the array before determining the array elements that are the sum of two other elements. Use a selection sort algorithm, discussed in this chapter to sort the array. Instructions and code for Programming Exercise 14 have been included for your convenience. Exercise 14 Write a program that prompts the user to enter 50 integers and stores them in an array. The program then determines and outputs which numbers in the array are...
I have a question about C++ programming Language class. I am confused with these terms and...
I have a question about C++ programming Language class. I am confused with these terms and what they are for. 1. Unix 2. Terminal 3. Git 4. CLOC 5. Linux Please explain each words and what's their jobs for C++. Also, if you know some good sources/websites, could you please provide me a link where I can learn how to use Unix, Terminal, Git, etc.
i have an array of strings, how do i sort them into a binary tree? C...
i have an array of strings, how do i sort them into a binary tree? C Program
Programming in C:- 1. Suppose that a variable 's' is an array of strings (i.e. a...
Programming in C:- 1. Suppose that a variable 's' is an array of strings (i.e. a pointer array where each element points to a string). Write an expression that obtains the length of the third string of 's'. You may assume that string.h has been included. 2. Consider the following function whose purpose is to return an array of 3 strings that are initialized to the strings in the character arrays s1, s2, and s3: #include <string.h> #include <stdlib.h> char**...
In C programming: Write a program that initializes an array-of-double and then copies the contents of...
In C programming: Write a program that initializes an array-of-double and then copies the contents of the array into another arrays. To make the copy, use a function with array notation. This function takes two arguments the name of the target array and the number of elements to be copied. That is, the function calls would look like this, given the following declarations: double source[5] ={1.1, 2.2, 3.3., 4.4, 5.5}; double target1[5]; double target2[5]; copyarr(source, target1, 5);
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT