// code to sort array of 16 numbers, but output isnt quite correct.
// must use pointers to array addresses
// final output is -451 993 384 201 89 77 38 28 16 12 7 1 0 -2 -5 -17
// as you can see -451 isn't in the right place.
output
-451 7 993 1 0 16 -5 12 89 28 77 384 -2 38 -17 201
-451 -17 993 7 1 16 0 12 89 28 77 384 -2 38 -5 201
-451 993 -17 7 1 16 0 12 89 28 77 384 -2 38 -5 201
-451 993 7 -17 1 16 0 12 89 28 77 384 -2 38 -5 201
-451 993 7 1 -17 16 0 12 89 28 77 384 -2 38 -5 201
-451 993 16 7 1 -17 0 12 89 28 77 384 -2 38 -5 201
-451 993 16 7 1 0 -17 12 89 28 77 384 -2 38 -5 201
-451 993 16 12 7 1 0 -17 89 28 77 384 -2 38 -5 201
-451 993 89 16 12 7 1 0 -17 28 77 384 -2 38 -5 201
-451 993 89 28 16 12 7 1 0 -17 77 384 -2 38 -5 201
-451 993 89 77 28 16 12 7 1 0 -17 384 -2 38 -5 201
-451 993 384 89 77 28 16 12 7 1 0 -17 -2 38 -5 201
-451 993 384 89 77 28 16 12 7 1 0 -2 -17 38 -5 201
-451 993 384 89 77 38 28 16 12 7 1 0 -2 -17 -5 201
-451 993 384 89 77 38 28 16 12 7 1 0 -2 -5 -17 201
-451 993 384 201 89 77 38 28 16 12 7 1 0 -2 -5 -17
total exchanges: 68
#include <stdio.h>
#define N 16
int xchg();
int main() {
int numbers[16] = {7, 1, 993, -5, 0, 16, -451, 12, 89, 28, 77, 384, -2, 38, -17, 201};
int cntr, cntr2, cntr3;
int chgNum;
for(cntr = 0; cntr < N; cntr++){
for(cntr2 = 1; cntr2 < N; cntr2++){
chgNum += xchg(&numbers[cntr], &numbers[cntr2]);
}
for(cntr3 = 0; cntr3 < N; cntr3++){
if(cntr3 == 15){
printf("%d", numbers[cntr3]);
}
else {
printf("%d ", numbers[cntr3]);
}
}
printf("\n");
}
printf("total exchanges: %d\n", chgNum);
return 0;
}
int xchg(int *p1, int *p2) {
int tmp = 0;
if(*p2 < *p1){
tmp = *p1;
*p1 = *p2;
*p2 = tmp;
return 1;
}
else {
return 0;
}
}
You need to change your loop in main.
for(cntr2 = cntr+1; cntr2 < N; cntr2++){
You might also want to check if xchg is giving you the direction ascending/descending sort order or if you need to invert your exchange condition.
Also: you forgot to initialize chgNum to zero.
Ok the problem is that the inner for loop starts at 1, after the first iteration it will be wrong it needs to start at the position that the first loop is at, i.e. cntr2 = cntr.
A few other things:
The forward declaration of the `xchng` function isn't correct.
The if else print statement doesn't do anything it will print the same thing no matter what.
If you are incrementing a number using `++` operator and it isn't being assigned to anything, always use `++(int)`, it requires fewer operations.
Finally, there are some nasty variable names, it doesn't hurt to be more descriptive. Also you define a macro which is the size of the array, but you don't define the array using it, int numbers[N] would make more sense as then at least you can see why the the macro is used in the loop.
Related
I was using svg code directly in a React component:
export const AdventurerToken = (props) => {
return (
<div style={props.style}>
<svg width="3vw" height="3vw" viewBox="0 0 99 119">
<metadata>
Created by potrace 1.15, written by Peter Selinger 2001-2017
</metadata>
<g transform="translate(0.000000,119.000000) scale(0.100000,-0.100000)"
fill={props.color} stroke="none">
<path d="M336 1174 c-10 -9 -16 -33 -16 -62 0 -26 -4 -52 -8 -59 -4 -6 -35 -17 -69 -23 -35 -7 -68 -19 -74 -26 -23 -28 50 -108 134 -145 20 -9 27 -19 27
-40 0 -23 -5 -30 -27 -35 -98 -21 -212 -74 -265 -122 -36 -32 -38 -38 -38 -92 0 -84 11 -92 134 -105 32 -3 62 -8 67 -11 11 -6 -12 -69 -50 -133 -77 -129
-114 -215 -115 -266 l-1 -50 168 -3 168 -2 57 90 c33 52 63 89 71 88 8 -2 35 -41 61 -88 l47 -85 172 -3 171 -2 0 43 c0 56 -35 151 -90 247 -86 149 -86 148
-74 160 6 6 43 14 83 17 98 8 121 26 121 97 0 62 -17 89 -84 133 -42 28 -157 74 -221 88 -29 6 -28 7 23 24 79 27 122 76 122 139 0 47 -72 102 -135 102 -18
0 -25 7 -30 33 -3 17 -13 49 -22 70 l-16 37 -138 0 c-112 0 -141 -3 -153 -16z"/>
</g>
</svg>
</div>
)
};
However Firefox doesn't accept "vw" units in sve width and height properties. Workaround would be using the svg as a source for an image element:
export const AdventurerToken = (props) => {
const color = props.color;
const adventurerSVG =
<svg width="100" height="100" viewBox="0 0 99 119">
<metadata>
Created by potrace 1.15, written by Peter Selinger 2001-2017
</metadata>
<g transform="translate(0.000000,119.000000) scale(0.100000,-0.100000)" fill={color} stroke="none">
<path d="M336 1174 c-10 -9 -16 -33 -16 -62 0 -26 -4 -52 -8 -59 -4 -6 -35 -17 -69 -23 -35 -7 -68 -19 -74 -26 -23 -28 50 -108 134 -145 20 -9 27 -19 27
-40 0 -23 -5 -30 -27 -35 -98 -21 -212 -74 -265 -122 -36 -32 -38 -38 -38 -92 0 -84 11 -92 134 -105 32 -3 62 -8 67 -11 11 -6 -12 -69 -50 -133 -77 -129
-114 -215 -115 -266 l-1 -50 168 -3 168 -2 57 90 c33 52 63 89 71 88 8 -2 35 -41 61 -88 l47 -85 172 -3 171 -2 0 43 c0 56 -35 151 -90 247 -86 149 -86 148
-74 160 6 6 43 14 83 17 98 8 121 26 121 97 0 62 -17 89 -84 133 -42 28 -157 74 -221 88 -29 6 -28 7 23 24 79 27 122 76 122 139 0 47 -72 102 -135 102 -18
0 -25 7 -30 33 -3 17 -13 49 -22 70 l-16 37 -138 0 c-112 0 -141 -3 -153 -16z"/>
</g>
</svg>
return (
<img style={{width: "10vw"}} src={adventurerSVG}/>
)
};
This does not work, as evidenced by https://codesandbox.io/s/dazzling-fire-m560j?file=/src/App.js - direct import works, import as a source of element does not. What am I doing wrong? Why is the image not loaded?
Workaround: use <div> instead of <img>.
Put svg code in a variable with 100% width and height, put variable inside with correct width / height:
export const AdventurerToken = (props) => {
const color = props.color;
const adventurerSVG =
<svg width="100%" height="100%" viewBox="0 0 99 119">
<metadata>
Created by potrace 1.15, written by Peter Selinger 2001-2017
</metadata>
<g transform="translate(0.000000,119.000000) scale(0.100000,-0.100000)" fill={color} stroke="none">
<path d="M336 1174 c-10 -9 -16 -33 -16 -62 0 -26 -4 -52 -8 -59 -4 -6 -35 -17 -69 -23 -35 -7 -68 -19 -74 -26 -23 -28 50 -108 134 -145 20 -9 27 -19 27
-40 0 -23 -5 -30 -27 -35 -98 -21 -212 -74 -265 -122 -36 -32 -38 -38 -38 -92 0 -84 11 -92 134 -105 32 -3 62 -8 67 -11 11 -6 -12 -69 -50 -133 -77 -129
-114 -215 -115 -266 l-1 -50 168 -3 168 -2 57 90 c33 52 63 89 71 88 8 -2 35 -41 61 -88 l47 -85 172 -3 171 -2 0 43 c0 56 -35 151 -90 247 -86 149 -86 148
-74 160 6 6 43 14 83 17 98 8 121 26 121 97 0 62 -17 89 -84 133 -42 28 -157 74 -221 88 -29 6 -28 7 23 24 79 27 122 76 122 139 0 47 -72 102 -135 102 -18
0 -25 7 -30 33 -3 17 -13 49 -22 70 l-16 37 -138 0 c-112 0 -141 -3 -153 -16z"/>
</g>
</svg>
return (
<div style={{width: "3vw"}}>
{adventurerSVG}
</div>
)
};
Works both in Firefox and Opera.
There are a couple of problems with your code
the SVG is not valid for use as an image because it's missing the SVG namespace declaration i.e. xmlns="http://www.w3.org/2000/svg"
you need to give the img element a data URI, not just a Node
So a working example looks like this...
import React from "react";
import ReactDOMServer from "react-dom/server";
import "./styles.css";
export default function App() {
return (
<div className="App">
{adventurerSVG}
<img src={'data:image/svg+xml,' + ReactDOMServer.renderToString(adventurerSVG)}/>
</div>
);
}
const adventurerSVG = (
<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100" viewBox="0 0 99 119">
<metadata>
Created by potrace 1.15, written by Peter Selinger 2001-2017
</metadata>
<g
transform="translate(0.000000,119.000000) scale(0.100000,-0.100000)"
fill="red"
stroke="none"
>
<path
d="M336 1174 c-10 -9 -16 -33 -16 -62 0 -26 -4 -52 -8 -59 -4 -6 -35 -17 -69 -23 -35 -7 -68 -19 -74 -26 -23 -28 50 -108 134 -145 20 -9 27 -19 27
-40 0 -23 -5 -30 -27 -35 -98 -21 -212 -74 -265 -122 -36 -32 -38 -38 -38 -92 0 -84 11 -92 134 -105 32 -3 62 -8 67 -11 11 -6 -12 -69 -50 -133 -77 -129
-114 -215 -115 -266 l-1 -50 168 -3 168 -2 57 90 c33 52 63 89 71 88 8 -2 35 -41 61 -88 l47 -85 172 -3 171 -2 0 43 c0 56 -35 151 -90 247 -86 149 -86 148
-74 160 6 6 43 14 83 17 98 8 121 26 121 97 0 62 -17 89 -84 133 -42 28 -157 74 -221 88 -29 6 -28 7 23 24 79 27 122 76 122 139 0 47 -72 102 -135 102 -18
0 -25 7 -30 33 -3 17 -13 49 -22 70 l-16 37 -138 0 c-112 0 -141 -3 -153 -16z"
/>
</g>
</svg>
);
I'm writing a simple Base64 decoding function in C. The output of this function looks correct (based on comparing the result with existing tools) when using the printf() debugging lines as below:
unsigned char * b64decode(char *line)
{
char *idx;
unsigned char *decode;
int i,j,len,dlen,hold = 0;
idx = strchr(line, '\n'); //index of newline
len = idx - line; //lenght of string
dlen = 3 * (len / 4); //decoded length
decode = malloc((sizeof(unsigned char) * dlen) + 1);
pad = 0;
for (i = 0; i <= len; i++) { //deindex from ASCII
line[i] = deindex(line[i]);
}
for(i = 0, j = u0; i < len; i++,j++) {
hold |= (line[i] << 18);
hold |= (line[++i] << 12);
hold |= (line[++i] << 6);
hold |= line[++i];
decode[j] = hold >> 16;
printf("%d ", decode[j]);
decode[++j] = (hold >> 8) & 0xFF;
printf("%d ", decode[j]);
decode[++j] = hold & 0xFF;
printf("%d ", decode[j]);
hold = 0;
}
if (pad) //terminate before padding
decode[dlen - pad] = '\0';
return decode;
}
Which produces the (partial) output:
29 66 31 77 11 15 2 31 79 19 78 60 26 105 101 31 73 28 14 78 19 1 11 7
78 27 1 22 69 54 0 30 1 73 100 32 84 29 29 67 51 83 78 101 82 6 0 71 84
28 13 69 77 7 4 12 83 18 60 12 30 8 73 26 9 17 79 20 76 33 26 71 43 0 5
29 71 89 17 4 9 0 100 38 7 83 0 55 22 6 12 26 23 65 29 1 82 84 48 95 0 32
19 10 5 71 79 18 72 8 69 78 101
The problem arises when I try to access this array after exiting the for loop with those printf() lines. This for loop (maximum i value is arbitrary) ...
for (i = 0; i < 100; i++) {
printf("%d ", decode[i]);
}
Produces this output...
29 66 31 77 11 15 2 31 79 19 78 60 26 105 101 31 73 28 14 78 19 1 11 7 78
27 1 22 69 54 0 30 1 73 100 32 84 29 29 67 51 83 78 101 82 6 0 71 84 28
13 69 77 7 4 12 83 18 60 12 30 8 73 26 9 17 79 20 76 33 26 71 43 0 5 29
71 89 17 4 9 0 100 38 7 83 0 55 22 6 12 26 23 65 29 1 82 84 48 95 0 32 19
10 5 71 79 168 187 158 23 131 127 0 0 168 187 158 23 131 127 0 0 0 0 0 0
0 0 0 0 65 0 0 0 0 0 0 0 88 187 158 23 131 127 0 0 88 187 158 23 131 127
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 64 0
0 0 0 0 0 0 16 16 0 0 0 0 0 0 72 85 73 102
Can anyone let me know what I'm doing wrong?
OK folks, found the issue. The function as shown here works, the issue turned out to be with a realloc() called on *decode in error in the function which handles my function above. Fixing this produced the correct results.
Guess this is a swift lesson in providing more context!
Hi I am trying to perform this on MATLAB
A =
64 2 3 61 60 6 7 57
9 55 54 12 13 51 50 16
17 47 46 20 21 43 42 24
40 26 27 37 36 30 31 33
32 34 35 29 28 38 39 25
41 23 22 44 45 19 18 48
49 15 14 52 53 11 10 56
8 58 59 5 4 62 63 1
I want to select values from A based on F
F =
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
-1 -1 -1 -1 -1 -1 -1 -1
0 0 0 0 0 0 0 0
-1 -1 -1 -1 -1 -1 -1 -1
-1 -1 -1 -1 -1 -1 -1 -1
-1 -1 -1 -1 -1 -1 -1 -1
I want this output
u =
40 26 27 37 36 30 31 33
41 23 22 44 45 19 18 48
49 15 14 52 53 11 10 56
8 58 59 5 4 62 63 1
This means that I want all the values corresponding to '-1' in a matrix. i tried u=A(F==-1) ... but it gives me a single column with all the values like this:
u =
40
41
49
8
26
23
15
58
27
22
14
59
37
44
52
5
36
45
53
4
30
19
11
62
31
18
10
63
33
48
56
1
This will do it:-
u=-A.*F;
u(all(u==0,2),:)=[] %Removing rows containing zeros
Another solution:-
u=reshape(A(F==-1),4,8)
#include<stdio.h>
main()
char i=13;
while(i)
{
i++;
}
printf("%d",i);
}
the out put turns out to be zero . how come does it happen
#include<stdio.h>
main()
char i=48;
if(i)
{
printf("%d",i);
}
}
this program run sucessfully and prints 48. isn't i considered a character o for which it is supposed to fail .How is i stored in the memory as a character or a number
You're missing a bracket after 'main()'. Your first code actually keeps counting up from 13, until it hits 127 (if, on your system, chars are represented as signed integers) because this is the largest positive integer for a char on such a system; then flips to -128 and keeps counting up to 0. At 0, you exit your loop, and print the result. To visualize what happens, copy this and try it out:
#include <stdio.h>
int main()
{
char i=13;
while(i){
i++;
printf("%d ", i);
}
printf("\n%d",i);
}
Output:
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 -128 -127 -126 -125 -124 -123 -122 -121 -120 -119 -118 -117 -116 -115 -114 -113 -112 -111 -110 -109 -108 -107 -106 -105 -104 -103 -102 -101 -100 -99 -98 -97 -96 -95 -94 -93 -92 -91 -90 -89 -88 -87 -86 -85 -84 -83 -82 -81 -80 -79 -78 -77 -76 -75 -74 -73 -72 -71 -70 -69 -68 -67 -66 -65 -64 -63 -62 -61 -60 -59 -58 -57 -56 -55 -54 -53 -52 -51 -50 -49 -48 -47 -46 -45 -44 -43 -42 -41 -40 -39 -38 -37 -36 -35 -34 -33 -32 -31 -30 -29 -28 -27 -26 -25 -24 -23 -22 -21 -20 -19 -18 -17 -16 -15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0
The positive to negative change has to do with the IEEE representation of integers on computers. You can find many related questions on SO if you google for them.
An if statement compares the numeric value, not the ASCII character that it happens to represent. Only the numeric value 0 will result in the if failing to execute in your second example.
I have a ScatterViewItem which contains a Canvas
<Ellipse x:Name="Outer_Ellipse" Fill="White" Width="200" Height="200"></Ellipse>
<Ellipse Fill="Red" Canvas.Top ="15" Canvas.Left="15" Canvas.Right="15" Canvas.Bottom="15" Width="170" Height="170" ></Ellipse>
</Canvas>
</s:ScatterViewItem>
Id like to provide a Custom Shape so that the default rectangle shape is not show (here is a picture of my current implementation .
I followed this example here link text and have consulted the Puzzle that comes with the SDK but I am unable to get it working, my ScatterViewItem is blank.
I defined a path in the SurfaceWindow.Resources
<Path x:Key="ScatterShape" Fill="Blue">
<Path.Data>
<EllipseGeometry
RadiusX="200"
RadiusY="200">
</EllipseGeometry>
</Path.Data>
</Path>
And copied the style attributes from the link above. I created my CustomShape.cs as instructed and then created the ScatterViewItem.
System.Windows.Shapes.Path path;
path = (System.Windows.Shapes.Path)Resources["ScatterShape"];
CustomShape poly = new CustomShape(path.Data);
ScatterViewItem item = new ScatterViewItem();
item.Content = poly;
item.CanScale = false;
Binding binding = new Binding();
binding.Source = poly;
item.SetBinding(ScatterViewItem.DataContextProperty, binding);
scatter.Items.Add(item)
Im slightly confused with the above code since my understanding with the line
item.Content = poly
would overwrite the content of the ScatterViewItem (i.e in my case the Canvas or in another case say an Image). For the time being I don't need to move or scale the ScatterView item so no shadows are neccessary I just simply want to remove the rectangular box.
You can achieve this by modifying the ControlTemplate for the ScatterViewItem.
If you want to remove all the visual features of the scatterview then I guess you could get away with an empty template:
<Style TargetType="{x:Type s:ScatterViewItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type s:ScatterViewItem}">
<ContentPresenter />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
The above template will change all scatterview items to have a blank template, but you could give it a x:Key="YourStyleName" and set the ItemContainerStyle of the ScatterView in question to only affect that scatter view.
Please note that if you use Expression Blend, you may need to add a reference to Microsoft.Surface.Presentation.Generic.dll to do this or Blend may crash when editing the template.
You can also remove the shadow, so that the ScatterViewItem is no longer visible.
Assuming that item is your ScatterViewItem:
item.ApplyTemplate();
item.Background = new SolidColorBrush(Colors.Transparent);
item.ShowsActivationEffects = false;
Microsoft.Surface.Presentation.Generic.SurfaceShadowChrome ssc;
ssc = item.Template.FindName("shadow", item) as Microsoft.Surface.Presentation.Generic.SurfaceShadowChrome;
ssc.Visibility = Visibility.Hidden;
I am having similar Problem.
Here is my code.
<s:ScatterView>
<s:ScatterView.Items>
<s:ScatterViewItem Height="1721" Width="2169">
<Canvas >
<Path Data="M0 2728 l0 -1012 28 26 c23 21 32 24 61 17 31 -6 40 -3 75 27 38 32 99 114 92 122 -6 5 -46 -21 -46 -30 0 -15 -56 -58 -75 -58 -29 0 -105 74 -105 102 0 12 14 37 32 55 31 32 32 34 20 78 -20 74 -17 82 29 89 22 4 44 4 49 1 13 -8 23 54 11 69 -8 10 -6 17 8 30 12 11 16 25 13 41 -4 24 -3 25 24 15 25 -10 31 -9 43 9 7 12 24 21 37 21 25 0 30 10 14 26 -5 5 -7 20 -3 32 7 29 34 25 48 -6 12 -27 20 -28 38 -1 13 18 24 20 74 18 32 -2 173 -2 314 -1 l256 2 12 113 c6 61 12 191 12 287 1 96 5 182 10 191 14 27 76 61 98 54 24 -7 29 -20 37 -103 10 -106 29 -115 65 -33 24 57 70 104 95 99 10 -1 29 -25 43 -53 37 -72 66 -79 141 -38 78 44 123 50 180 25 88 -38 122 -38 197 0 64 31 75 33 193 36 77 3 145 -1 177 -9 45 -10 61 -10 115 5 135 37 121 41 253 -81 64 -59 124 -111 133 -114 27 -10 473 0 515 12 47 12 84 47 92 86 22 99 71 155 143 159 27 2 79 17 118 34 46 21 97 35 153 41 64 7 100 18 150 44 36 19 86 37 111 41 25 4 65 17 90 29 52 26 113 39 140 30 29 -9 25 -49 -10 -89 -25 -28 -29 -38 -21 -59 5 -14 15 -28 21 -32 12 -8 3 -73 -13 -94 -16 -19 17 -43 103 -76 66 -25 80 -27 188 -23 109 4 121 6 176 36 32 18 67 32 77 32 25 0 24 12 -1 35 -13 12 -20 29 -18 44 2 21 10 27 38 32 39 6 102 40 140 75 23 21 30 22 64 13 52 -14 82 -5 94 30 8 22 18 30 45 35 37 7 73 38 63 55 -9 14 -94 40 -158 47 l-58 7 0 87 c0 48 -3 90 -7 93 -3 4 -5 35 -4 70 3 73 24 102 105 142 l50 25 -2592 0 -2592 0 0 -1012z" Stroke="Black"></Path>
....
....
</Canvas>
</s:ScatterViewItem>
</s:ScatterView.Items>
</s:ScatterView>
There are so many shapes in the canvas and the size of the canvas is also much higher.
so when i run the application, The canvas doesn't get re size when i re size the ScatterViewItem. And Event the Canvas i shown outside of the ScatterViewItem.