Simple structure initialization in Swift (4)? - arrays

I'm new to Swift and trying to port some code. I have this from an old project:
typedef struct {
float Position[3];
float Normal[3];
float TexCoord[2]; // New
} iconVertex;
const iconVertex iconVertices[] = {
{{0.0,0.0, 0.0}, {0, 0, 1.0}, {0, 0}},
{{1.0, 0.0, 0.0}, {0, 0, 1.0}, {1, 0}},
{{0.0, 1.0, 0.0}, {0, 0, 1.0}, {0, 1}},
{{1.0, 1.0, 0.0}, {0, 0, 1.0}, {1, 1}},
};
Is there any way to do that same array initialization in Swift?
Thanks!

In Swift you can use Structs to define your objects and create an init method that receives the parameters that you need to initialise.
struct IconVertex {
var position: [Double]
var normal: [Double]
var textCoord: [Double]
init(position: [Double], normal: [Double], textCoord: [Double]) {
self.position = position
self.normal = normal
self.textCoord = textCoord
}
}
let iconVertices: [IconVertex] = [
IconVertex(position: [0.0,0.0, 0.0], normal: [0, 0, 1.0], textCoord: [0, 0]),
IconVertex(position: [1.0, 0.0, 0.0], normal: [0, 0, 1.0], textCoord: [1, 0]),
IconVertex(position: [0.0, 1.0, 0.0], normal: [0, 0, 1.0], textCoord: [0, 1]),
IconVertex(position: [1.0, 1.0, 0.0], normal: [0, 0, 1.0], textCoord: [1, 1])]

Related

How Do I Fix This Numpy Error: Too Many Indices For Array?

I am working with some old code that works in Python2 but raises an error in Python3. The error stems from an np array in the module. The array is below.
if I assign the np array to a variable called test, and call test[:,0], I get an error "IndexError: too many indices for array."
The byte array is created using an np.asarray() function on bytes coming from a calibration table on a piece of hardware. This error is particularly confusing to me, as it works perfectly fine in Python2.
How can I fix this issue?
>>>test=b'[[0, 0, 0], [0.0, 0.01, -1.0], [0.1, 0.01, 8.0], [0.2, 0.02, 10.0], [0.3, 0.02, 12.0], [0.4, 0.03, 14.0], [0.5, 0.04, 16.0], [0.6, 0.05, 18.0], [0.7, 0.06, 19.0], [0.8, 0.08, 21.0], [0.9, 0.11, 23.0], [1.0, 0.24, 24.0], [1.1, 3.82, 25.0], [1.2, 9.09, 26.0], [1.3, 14.36, 27.0], [1.4, 19.78, 28.0], [1.5, 25.11, 30.0], [1.6, 30.57, 31.0], [1.7, 36.01, 31.0], [1.8, 41.51, 33.0], [1.9, 47.07, 34.0], [2.0, 52.57, 35.0], [2.1, 58.13, 36.0], [2.2, 63.74, 37.0], [2.3, 69.46, 37.0], [2.4, 74.89, 38.0], [2.5, 80.63, 39.0]]'
>>>test2 = np.asarray(test)
>>>test2[:,0]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: too many indices for array
EDIT: Added code for clarification. Also, the issue seems to be that in Python3, type(test) = bytes, but in python2 type(test) = list.
Your code sample is incomplete. I suppose it shoud be:
test = b'[...]'
(... stands for your content between brackets, I added ' at the end).
Note that test is of bytes type.
When you run test2.ndim the result is 0, so (in the recent version of
Numpy) it is a 0-D array.
To get its content (a scalar), you can run: test2.item(), getting
back a bytes object:
b'[...]'
You can transcode it into an "ordinary" (UTF-8) string, running:
test2.item().decode('utf-8')
but it is still a string, not an array.
To get "ordinary" 2-D Numpy array, you can import ast and then run:
test3 = np.array(ast.literal_eval(test2.item().decode('utf-8')))
(ast.literal_eval is more safe than "ordinary" eval) or starting
from test:
test3 = np.array(ast.literal_eval(test.decode('utf-8')))
Now you can run: test3[:,0], getting what you want, i.e.:
array([0. , 0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. , 1.1,
1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2. , 2.1, 2.2, 2.3, 2.4,
2.5])
Are you sure this working in Py2?
In [290]: test=b'[[0, 0, 0], [0.0, 0.01, -1.0], [0.1, 0.01, 8.0], [0.2, 0.02, 10.0], [0.3
...: , 0.02, 12.0], [0.4, 0.03, 14.0], [0.5, 0.04, 16.0], [0.6, 0.05, 18.0], [0.7, 0
...: .06, 19.0], [0.8, 0.08, 21.0], [0.9, 0.11, 23.0], [1.0, 0.24, 24.0], [1.1, 3.82
...: , 25.0], [1.2, 9.09, 26.0], [1.3, 14.36, 27.0], [1.4, 19.78, 28.0], [1.5, 25.11
...: , 30.0], [1.6, 30.57, 31.0], [1.7, 36.01, 31.0], [1.8, 41.51, 33.0], [1.9, 47.0
...: 7, 34.0], [2.0, 52.57, 35.0], [2.1, 58.13, 36.0], [2.2, 63.74, 37.0], [2.3, 69.
...: 46, 37.0], [2.4, 74.89, 38.0], [2.5, 80.63, 39.0]]'
In [291]: test
Out[291]: b'[[0, 0, 0], [0.0, 0.01, -1.0], [0.1, 0.01, 8.0], [0.2, 0.02, 10.0], [0.3, 0.02, 12.0], [0.4, 0.03, 14.0], [0.5, 0.04, 16.0], [0.6, 0.05, 18.0], [0.7, 0.06, 19.0], [0.8, 0.08, 21.0], [0.9, 0.11, 23.0], [1.0, 0.24, 24.0], [1.1, 3.82, 25.0], [1.2, 9.09, 26.0], [1.3, 14.36, 27.0], [1.4, 19.78, 28.0], [1.5, 25.11, 30.0], [1.6, 30.57, 31.0], [1.7, 36.01, 31.0], [1.8, 41.51, 33.0], [1.9, 47.07, 34.0], [2.0, 52.57, 35.0], [2.1, 58.13, 36.0], [2.2, 63.74, 37.0], [2.3, 69.46, 37.0], [2.4, 74.89, 38.0], [2.5, 80.63, 39.0]]'
In [292]: np.asarray(test)
Out[292]:
array(b'[[0, 0, 0], [0.0, 0.01, -1.0], [0.1, 0.01, 8.0], [0.2, 0.02, 10.0], [0.3, 0.02, 12.0], [0.4, 0.03, 14.0], [0.5, 0.04, 16.0], [0.6, 0.05, 18.0], [0.7, 0.06, 19.0], [0.8, 0.08, 21.0], [0.9, 0.11, 23.0], [1.0, 0.24, 24.0], [1.1, 3.82, 25.0], [1.2, 9.09, 26.0], [1.3, 14.36, 27.0], [1.4, 19.78, 28.0], [1.5, 25.11, 30.0], [1.6, 30.57, 31.0], [1.7, 36.01, 31.0], [1.8, 41.51, 33.0], [1.9, 47.07, 34.0], [2.0, 52.57, 35.0], [2.1, 58.13, 36.0], [2.2, 63.74, 37.0], [2.3, 69.46, 37.0], [2.4, 74.89, 38.0], [2.5, 80.63, 39.0]]',
dtype='|S517')
Did you look at test2 before trying to use it? You have just wrapped one big string in np.array; look at the shape and dtype.
You can make an array from the text if applied as:
In [293]: np.array([[0, 0, 0], [0.0, 0.01, -1.0], [0.1, 0.01, 8.0], [0.2, 0.02, 10.0], [0
...: .3, 0.02, 12.0], [0.4, 0.03, 14.0], [0.5, 0.04, 16.0], [0.6, 0.05, 18.0], [0.7,
....
...: 9.46, 37.0], [2.4, 74.89, 38.0], [2.5, 80.63, 39.0]])
Out[293]:
array([[ 0.000e+00, 0.000e+00, 0.000e+00],
[ 0.000e+00, 1.000e-02, -1.000e+00],
[ 1.000e-01, 1.000e-02, 8.000e+00],
[ 2.000e-01, 2.000e-02, 1.000e+01],
....
[ 2.500e+00, 8.063e+01, 3.900e+01]])
But this is quite different from passing a string (even a py2 bytestring) to np.array.
You could use eval to convert the bytestring to a list of lists, and then make the array from that:
In [298]: eval(test)
Out[298]:
[[0, 0, 0],
[0.0, 0.01, -1.0],
[0.1, 0.01, 8.0],
[0.2, 0.02, 10.0],
...
[2.5, 80.63, 39.0]]
In [299]: np.array(eval(test))
Out[299]:
array([[ 0.000e+00, 0.000e+00, 0.000e+00],
[ 0.000e+00, 1.000e-02, -1.000e+00],
[ 1.000e-01, 1.000e-02, 8.000e+00],
...
[ 2.500e+00, 8.063e+01, 3.900e+01]])

Array after an element is > 0 change all elements to that value until next non zero value

I have an array that is for example
[0.0, 0.0, 55.0, 0.0, 0.0, 55.0, 55.0, 22.0, 0.0, 55.0]
How can I change the element with 0 value if the previous value > 0? so
[0.0, 0.0, 55.0, 0.0, 0.0, 55.0, 55.0, 22.0, 0.0, 55.0]
becomes
[0.0, 0.0, 55.0, 55.0, 55.0, 55.0, 55.0, 22.0, 22.0, 55.0]
I have tried the following and it removes the zeros and nothing else.
func weightArrayRemovedZero(array: [Double])->[Double]{
var arrayToAlter = [Double]()
for(index,item) in array.enumerated() {
print("The \(item) is at index:\(index)")
if item == 0.0 {
if index > 0 && index < array.count - 1 {
if array[index - 1] != 0.0 {
let nonZeroElement = array[index - 1]
arrayToAlter.append(nonZeroElement)
}
}
} else {
arrayToAlter.append(item)
}
}
return arrayToAlter
}
map seems to be the natural approach to me:
var last = 0.0
let mapped = values.map { elem -> (Double) in
last = elem > 0.0 ? elem : last
return last
}
Generally speaking, map is your go to when you want to change one collection into another collection with a one-to-one element mapping.
Try this out
var array = [0.0, 0.0, 55.0, 0.0, 0.0, 55.0, 55.0, 22.0, 0.0, 55.0]
array.enumerated().forEach {
array[$0] = $1 == 0 &&
array.indices.contains($0 - 1) &&
array[$0 - 1] > 0 ? array[$0 - 1] : $1
}
print(array)
Another option:
var array = [0.0, 0.0, 55.0, 0.0, 0.0, 55.0, 55.0, 22.0, 0.0, 55.0]
for i in array.indices {
if i == 0 { continue }
if array[i] == 0 && array[i-1] != 0 {
array[i] = array[i-1]
}
}
// [0, 0, 55, 55, 55, 55, 55, 22, 22, 55]

OpenGL, how to give sides of pyramid solid colors

Right now I am learning OpenGL and I want to draw a pyramid where each side has a different color. The issue I am having is that I only seem to be able to assign colors to the vertexes and not the sides. Therefore each side has a gradient effect rather than being a solid color. How can I give my sides solid color?
void init_buffer()
{
glGenBuffers(1, &(b.trifan));
glBindBuffer(GL_ARRAY_BUFFER, b.trifan);
GLfloat trifan[6][3] =
{
{0.0, 1.0, 0.0},
{1.0, 0.0, 1.0},
{-1.0, 0.0, 1.0},
{-1.0, 0.0, -1.0},
{1.0, 0.0, -1.0},
{1.0, 0.0, 1.0}
};
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 3 * 6, trifan, GL_STATIC_COPY);
//base of the pyramid as 2 triangles
glGenBuffers(1, &(b.tribase));
glBindBuffer(GL_ARRAY_BUFFER, b.tribase);
GLfloat tribase[4][3] =
{
{1.0, 0.0, -1.0},
{-1.0, 0.0, -1.0},
{1.0, 0.0, 1.0},
{-1.0, 0.0, 1.0}
};
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 4 * 3, tribase, GL_STATIC_COPY);
//colors
glGenBuffers(1, &b.colors);
glBindBuffer(GL_ARRAY_BUFFER, b.colors);
GLfloat colors[6][3] =
{
{1.0, 0.0, 0.0},
{1.0, 0.0, 0.0},
{1.0, 0.0, 0.0},
{1.0, 0.0, 1.0},
{0.5, 0.0, 1.0},
{1.0, 0.0, 0.5},
};
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 3 * 6, colors, GL_STATIC_COPY);
check_errors();
}
...
//Inside my display function
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
{
glBindBuffer(GL_ARRAY_BUFFER, b.trifan);
glVertexPointer(3, GL_FLOAT, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, b.colors);
glColorPointer(3, GL_FLOAT, 0, 0);
glDrawArrays(GL_TRIANGLE_FAN, 0, 6);
glBindBuffer(GL_ARRAY_BUFFER, b.tribase);
glVertexPointer(3, GL_FLOAT, 0, 0);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
You can duplicate the vertices for each face as already suggested in the comments. The same is also achievable with the use of different textures (bitmaps) for each face, without duplicating the vertices.
Refer to
http://oglsuperbible5.googlecode.com/svn/trunk/Src/Chapter05/Pyramid/Pyramid.cpp
Figure at https://www.scss.tcd.ie/Michael.Manzke/CS7055/Lab2/SuperBible.4th.Ed.Ch8-9.pdf
Note: The superbible reference only has one texture mapped on all faces, so additional code is to be written to map different textures to different faces.

webgl drawElements:attribs not setup correctly (repeated n times)

So, I've begun a rather instantaneous trip into the world of visual, 3-d programming. I'm currently heavily invested in webgl with a rather strong background in JavaScript and most web-oriented languages but this is my first graphics language.
While trying to draw my first rather simple shape, I've run into an error I can't seem to locate a solution for. It reads in chrome as a:
WebGL: INVALID_OPERATION: drawElements:attribs not setup correctly (repeated n times)
where n is a number that varies seemingly randomly. The code in question is here:
var tessVertexPositionBuffer;
var tessVertexColorBuffer;
var tessVertexIndexBuffer;
function initBuffers () {
tessVertexPositionBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, tessVertexPositionBuffer);
var vertices = [
//innerfront
-1.0, 1.0, 1.0,
1.0, 1.0, 1.0,
1.0, -1.0, 1.0,
-1.0, -1.0, 1.0,
//innerleft
-1.0, 1.0, 1.0,
-1.0, -1.0, 1.0,
-1.0, 1.0, -1.0,
-1.0, -1.0, -1.0,
//innerback
-1.0, 1.0, -1.0,
1.0, 1.0, -1.0,
-1.0, -1.0, -1.0,
1.0, -1.0, -1.0,
//innerright
1.0, 1.0, 1.0,
1.0, -1.0, 1.0,
1.0, 1.0, -1.0,
1.0, -1.0, -1.0,
//topfront
-1.0, 1.0, 1.0,
1.0, 1.0, 1.0,
-2.0, 2.0, 2.0,
2.0, 2.0, 2.0,
//topleft
-1.0, 1.0, 1.0,
-2.0, 2.0, 2.0,
-1.0, 1.0, -1.0,
-2.0, 2.0, -2.0,
//topback
-1.0, 1.0, -1.0,
1.0, 1.0, -1.0,
-2.0, 2.0, -2.0,
2.0, 2.0, -2.0,
//topright
1.0, 1.0, 1.0,
2.0, 2.0, 2.0,
1.0, 1.0, -1.0,
2.0, 2.0, -2.0,
//outerfront
-2.0, 2.0, 2.0,
2.0, 2.0, 2.0,
-2.0, -2.0, 2.0,
2.0, -2.0, 2.0,
//outerleft
-2.0, 2.0, 2.0,
-2.0, -2.0, 2.0,
-2.0, 2.0, -2.0,
-2.0, -2.0, -2.0,
//outerback
-2.0, 2.0, -2.0,
2.0, 2.0, -2.0,
-2.0, -2.0, -2.0,
2.0, -2.0, -2.0,
//outerright
2.0, 2.0, 2.0,
2.0, -2.0, 2.0,
2.0, 2.0, -2.0,
2.0, -2.0, -2.0,
//bottomfront
2.0, 2.0, 2.0,
-2.0, 2.0, 2.0,
-2.0, -2.0, 2.0,
2.0, -2.0, 2.0,
//bottomleft
-1.0, -1.0, 1.0,
-2.0, -2.0, 2.0,
-1.0, -1.0, -1.0,
-2.0, -2.0, -2.0,
//bottomback
-1.0, -1.0, -1.0,
1.0, -1.0, -1.0,
-2.0, -2.0, -2.0,
2.0, -2.0, -2.0,
//bottomright
1.0, -1.0, 1.0,
2.0, -2.0, 2.0,
1.0, -1.0, -1.0,
2.0, -2.0, -2.0
];
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
tessVertexPositionBuffer.itemSize = 3;
tessVertexPositionBuffer.numItems = 64;
tessVertexColorBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, tessVertexColorBuffer);
var colors = [
[0.7, 0.7, 0.7, 1.0], //all inner sides
[0.7, 0.7, 0.7, 1.0],
[0.7, 0.7, 0.7, 1.0],
[0.7, 0.7, 0.7, 1.0],
[0.7, 0.0, 0.7, 1.0], //all top sides
[0.7, 0.0, 0.7, 1.0],
[0.7, 0.0, 0.7, 1.0],
[0.7, 0.0, 0.7, 1.0],
[0.7, 0.7, 0.0, 1.0], //all outer sides
[0.7, 0.7, 0.0, 1.0],
[0.7, 0.7, 0.0, 1.0],
[0.7, 0.7, 0.0, 1.0],
[0.0, 0.7, 0.7, 1.0], //all bottom sides
[0.0, 0.7, 0.7, 1.0],
[0.0, 0.7, 0.7, 1.0],
[0.0, 0.7, 0.7, 1.0],
];
var unpackedColors = [];
for (var i in colors) {
var color = colors[i];
for (var j=0; j< 4; j++) {
unpackedColors = unpackedColors.concat(color);
}
}
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(unpackedColors), gl.STATIC_DRAW);
tessVertexColorBuffer.itemSize = 4;
tessVertexColorBuffer.numItems = 64;
tessVertexIndexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, tessVertexIndexBuffer);
var tessVertexIndices = [
0, 1, 2, 0, 2, 3,
4, 5, 6, 5, 6, 7,
8, 9, 10, 9, 10, 11,
12, 13, 14, 13, 14, 15,
16, 17, 18, 17, 18, 19,
20, 21, 22, 21, 22, 23,
24, 25, 26, 25, 26, 27,
28, 29, 30, 29, 30, 31,
32, 33, 34, 33, 34, 35,
36, 37, 38, 37, 38, 39,
40, 41, 42, 41, 42, 43,
44, 45, 46, 45, 46, 47,
48, 49, 50, 48, 50, 51,
52, 53, 54, 53, 54, 55,
56, 57, 58, 57, 58, 59,
60, 61, 62, 61, 62, 63
];
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(tessVertexIndices), gl.STATIC_DRAW);
tessVertexIndexBuffer.itemSize = 1;
tessVertexIndexBuffer.numItems = 96;
}
and the actual drawing of the buffers is here:
gl.bindBuffer(gl.ARRAY_BUFFER, tessVertexPositionBuffer);
gl.vertexAttribPointer(shaderProgram.vetexPositionAttribute, tessVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ARRAY_BUFFER, tessVertexColorBuffer);
gl.vertexAttribPointer(shaderProgram.vertexColorAttribute, tessVertexColorBuffer.itemSize, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, tessVertexIndexBuffer);
setMatrixUniforms();
gl.drawElements(gl.TRIANGLES, tessVertexIndexBuffer.numItems, gl.UNSIGNED_SHORT, 0);
Now, this is relatively verbatim from learningwebgl, I'm really just trying to draw a simple shape. I'm relatively certain that my problem lies in my buffer types because I honestly don't uderstand much about them (and most literature on webGl I've found is either novice with a general understanding of the language or of the HYPERSUPERPRO variety).
I've checked over the actual vertex positions/colors/indices multiple times and unless I've just become familiar with the code enough that I'm blind to the simple errors, I can't find an error there.
Looks like your problem is just a small typo. The second line of your "actual drawing of buffers" code should be he following:
gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, tessVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
You were missing the first r in vertexPositionAttribute on that line.
I modified the lesson 4 LearningWebGL tutorial to use your code (with the fixed typo) and I have a link to it here.
If you notice there is a blue triangle and a yellow triangle being drawn at the exact same position in space which causes some flickering. I'm not sure if that was intentional.

How to get a structure array to be a single string

I'm having a tough time figuring out how to get this structure array to appear as a single string "c_str" when I use the printf function. As of now I only can only get c_str to be one part of the line structure array, in this case the 24th line. When I use print(c_str); i would like the output to display all of the data that is in the code. It needs to be stored as a string because I have a function that needs to access n,m,gnm,hnm,dgnm, and dhnm.
Thank you for the help
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int main()
{
int i=0,n[90],m[90];
float gnm[90],hnm[90],dgnm[90],dhnm[90];
static char c_str[90];
struct wmm
{
int n;
int m;
float gnm;
float hnm;
float dgnm;
float dhnm;
} book[90]= {{1, 0, -29496.6, 0.0, 11.6, 0.0},
{1, 1, -1586.3, 4944.4, 16.5, -25.9},
{2, 0, -2396.6, 0.0, -12.1, 0.0},
{2, 1, 3026.1, -2707.7, -4.4, -22.5},
{2, 2, 1668.6, -576.1, 1.9, -11.8},
{3, 0, 1340.1, 0.0, 0.4, 0.0},
{3, 1, -2326.2, -160.2, -4.1, 7.3},
{3, 2, 1231.9, 251.9, -2.9, -3.9},
{3, 3, 634.0, -536.6, -7.7, -2.6},
{4, 0, 912.6, 0.0, -1.8, 0.0},
{4, 1, 808.9, 286.4, 2.3, 1.1},
{4, 2, 166.7, -211.2, -8.7, 2.7},
{4, 3, -357.1, 164.3, 4.6, 3.9},
{4, 4, 89.4, -309.1, -2.1, -0.8},
{5, 0, -230.9, 0.0, -1.0, 0.0},
{5, 1, 357.2, 44.6, 0.6, 0.4},
{5, 2, 200.3, 188.9, -1.8, 1.8},
{5, 3, -141.1, -118.2, -1.0, 1.2},
{5, 4, -163.0, 0.0, 0.9, 4.0},
{5, 5, -7.8, 100.9, 1.0, -0.6},
{6, 0, 72.8, 0.0, -0.2, 0.0},
{6, 1, 68.6, -20.8, -0.2, -0.2},
{6, 2, 76.0, 44.1, -0.1, -2.1},
{6, 3, -141.4, 61.5 , 2.0, -0.4},
{6, 4, -22.8, -66.3, -1.7, -0.6},
{6, 5, 13.2, 3.1, -0.3, 0.5},
{6, 6, -77.9, 55.0, 1.7, 0.9},
{7, 0, 80.5, 0.0, 0.1, 0.0},
{7, 1, -75.1, -57.9, -0.1, 0.7},
{7, 2, -4.7, -21.1, -0.6, 0.3},
{7, 3, 45.3, 6.5, 1.3, -0.1},
{7, 4, 13.9, 24.9, 0.4, -0.1},
{7, 5, 10.4, 7.0, 0.3, -0.8},
{7, 6, 1.7, -27.7, -0.7, -0.3},
{7, 7, 4.9, -3.3, 0.6, 0.3},
{8, 0, 24.4, 0.0, -0.1, 0.0},
{8, 1, 8.1, 11.0, 0.1, -0.1},
{8, 2, -14.5, -20.0, -0.6, 0.2},
{8, 3, -5.6, 11.9, 0.2, 0.4},
{8, 4, -19.3, -17.4, -0.2, 0.4},
{8, 5, 11.5, 16.7, 0.3, 0.1},
{8, 6, 10.9, 7.0, 0.3, -0.1},
{8, 7, -14.1, -10.8, -0.6, 0.4},
{8, 8, -3.7, 1.7, 0.2, 0.3},
{9, 0, 5.4, 0.0, 0.0, 0.0},
{9, 1, 9.4, -20.5, -0.1, 0.0},
{9, 2, 3.4, 11.5, 0.0, -0.2},
{9, 3, -5.2, 12.8, 0.3, 0.0},
{9, 4, 3.1, -7.2, -0.4, -0.1},
{9, 5, -12.4, -7.4, -0.3, 0.1},
{9, 6, -0.7, 8.0, 0.1, 0.0},
{9, 7, 8.4, 2.1, -0.1, -0.2},
{9, 8, -8.5, -6.1, -0.4, 0.3},
{9, 9, -10.1, 7.0, -0.2, 0.2},
{10, 0, -2.0, 0.0, 0.0, 0.0},
{10, 1, -6.3, 2.8, 0.0, 0.1},
{10, 2 , 0.9 , -0.1 , -0.1 , -0.1},
{10, 3, -1.1, 4.7, 0.2, 0.0},
{10, 4, -0.2, 4.4, 0.0, -0.1},
{10, 5, 2.5, -7.2, -0.1, -0.1},
{10, 6, -0.3, -1.0 , -0.2 , 0.0},
{10, 7, 2.2, -3.9, 0.0, -0.1},
{10, 8, 3.1, -2.0, -0.1, -0.2},
{10, 9, -1.0, -2.0, -0.2, 0.0},
{10, 10, -2.8, -8.3, -0.2, -0.1},
{11, 0, 3.0, 0.0, 0.0, 0.0},
{11, 1, -1.5, 0.2, 0.0, 0.0},
{11, 2, -2.1, 1.7, 0.0, 0.1},
{11, 3, 1.7, -0.6, 0.1, 0.0},
{11, 4, -0.5, -1.8, 0.0, 0.1},
{11, 5, 0.5, 0.9, 0.0, 0.0},
{11, 6, -0.8, -0.4, 0.0, 0.1},
{11, 7, 0.4, -2.5, 0.0, 0.0},
{11, 8, 1.8, -1.3, 0.0, -0.1},
{11, 9, 0.1, -2.1, 0.0, -0.1},
{11, 10, 0.7, -1.9, -0.1, 0.0},
{11, 11, 3.8, -1.8, 0.0, -0.1},
{12, 0, -2.2, 0.0, 0.0, 0.0},
{12, 1, -0.2, -0.9, 0.0, 0.0},
{12, 2, 0.3, 0.3, 0.1, 0.0},
{12, 3, 1.0 , 2.1 , 0.1 , 0.0},
{12, 4, -0.6, -2.5, -0.1, 0.0},
{12, 5, 0.9, 0.5, 0.0, 0.0},
{12, 6, -0.1, 0.6, 0.0, 0.1},
{12, 7, 0.5, 0.0, 0.0, 0.0},
{12, 8, -0.4, 0.1, 0.0, 0.0},
{12, 9, -0.4, 0.3, 0.0, 0.0},
{12, 10, 0.2, -0.9, 0.0, 0.0},
{12, 11, -0.8, -0.2, -0.1, 0.0},
{12, 12, 0.0, 0.9, 0.1, 0.0}};
sprintf(c_str, " %d %d %lf %lf %lf %lf" ,book[25].n, book[25].m , book[25].gnm , book[25].hnm, book[25].dgnm, book[25].dhnm);
getchar();
return 0;
}
If you are trying to copy all of the data in the struct into c_str, you only need to wrap sprintf() in a loop. Note that you should really switch to snprintf() to avoid accidentally introducing a buffer-overflow.
Since sprintf() and snprintf() return the number of character that they add to the string, you can easily keep track of the next unused location in the string:
int i, n = 90, buf_size = 90, offset = 0;
for (i = 0; i < n && offset < buf_size; ++i) {
offset += snprintf(c_str + offset, buf_size - offset, " %d %d %lf %lf %lf %lf", book[i].n, book[i].m , book[i].gnm , book[i].hnm, book[i].dgnm, book[i].dhnm);
}
If you were to repeatedly pass c_str as the first argument, it would only contain the last line.

Resources