Each triangle is described by a triangle_t data structure. All the functions below accept a pointer to a triangle data structure as input. Each function needs to access and modify the fields of the data structure as appropriate.

Note that triangle scale is specified as a floating-point number, while the triangle coordinates are integer variables. You will need to think about conversions between integer and floating-point numbers to make the scaling function work properly. When scaling, translation, and drawing works, embark on the code for filling the triangles.

i managed to go this far, but still no tea pot appear on my screen, i think i am doing something wrong can you help me please , i think there is something to with triangle scale is specified as a floating-point number, while the triangle coordinates are integer variables. here what i worte so far :

Code:

Code:

`#include<stdlib.h>`

#include<stdio.h>

#include<SDL.h>

#include"triangle.h"

#include"drawline.h"

#defineTRIANGLE_PENCOLOR0xBBBB0000

/*

* Print triangle coordinates along with a message

*/

void print_triangle(triangle_t *triangle, char *msg)

{

printf("%s: %d,%d - %d,%d - %d,%d\n",

msg,

triangle->x1, triangle->y1,

triangle->x2, triangle->y2,

triangle->x3, triangle->y3);

}

/*

* Return 0 if triangle coordinates are outside the surface boundary. 1 otherwise.

*/

int sanity_check_triangle(SDL_Surface *surface, triangle_t *triangle)

{

if (triangle->sx1 < 0 || triangle->sx1 >= surface->w ||

triangle->sx2 < 0 || triangle->sx2 >= surface->w ||

triangle->sx3 < 0 || triangle->sx3 >= surface->w ||

triangle->sy1 < 0 || triangle->sy1 >= surface->h ||

triangle->sy2 < 0 || triangle->sy2 >= surface->h ||

triangle->sy3 < 0 || triangle->sy3 >= surface->h) {

return 0;

} else {

return 1;

}

}

/*

* Scale triangle, altering the on-screen coordinates(e.g. triangle->sx1)

*/

void scale_triangle(triangle_t *triangle)

{

// TODO: Replace the code below with code that scales each triangle coordinate.

// The scaling factor is specified in triangle->scale.

// Remember that this function MUST write to the on-surface coordinates.

// Do not alter the model coordinates.

float dx1=0, dx2=0, dx3=0;

float dy1=0, dy2=0, dy3=0;

triangle->sx1 = triangle->x1;

triangle->sx2 = triangle->x2;

triangle->sx3 = triangle->x3;

triangle->sy1 = triangle->y2;

triangle->sy2 = triangle->y1;

triangle->sy3 = triangle->y3;

dx1=((triangle->x2-triangle->x1)*triangle->scale);

dx2=((triangle->x3-triangle->x2)*triangle->scale);

dx3= ((triangle->x1-triangle->x3)*triangle->scale);

dy1=((triangle->y2-triangle->y1)*triangle->scale);

dy2= ((triangle->y3-triangle->y2)*triangle->scale);

dy3= ((triangle->y1-triangle->y3)*triangle->scale);

triangle->sx1= triangle->sx3+dx3;

triangle->sx2= triangle->sx1+dx1;

triangle->sx3=triangle->sx2+dx2;

triangle->sy1= triangle->sy3+dy3;

triangle->sy2= triangle->sy1+dy1;

triangle->sy3=triangle->sy2+dy2;

}

/*

* Move the triangle to the center of the surface,

* altering the on-screen coordinates(e.g. triangle->sx1)

*/

void translate_triangle(triangle_t *triangle)

{

// TODO: Insert code that moves the triangle on the surface.

// The translation coordinates are specified in triangle->tx and triangle->ty.

// Remember to use the on-surface coordinates (triangle->sx1, etc.)

triangle->tx = 1024/2;

triangle->ty = 768/2;

triangle->sx1=(triangle->tx+triangle->sx1);

triangle->sx2 = (triangle->tx+triangle->sx2);

triangle->sx3 =(triangle->tx+triangle->sx3);

triangle->sy1 = (triangle->ty+triangle->sy1);

triangle->sy2= (triangle->ty+triangle->sy2);

triangle->sy3 = (triangle->ty+triangle->sy3);

}

/*

* Calculate the triangle bounding box,

* altering fields of the triangle's rect(e.g. triangle->rect.x)

*/

void calculate_triangle_bounding_box(triangle_t *triangle)

{

// TODO: Insert code that calculates the bounding box of a triangle.

// Remember to use the on-surface coordinates (triangle->sx1, etc.)

// The bounding box coordinates should be written to

// triangle->rect.x, triangle->rect.y, triangle->rect.w, triangle->rect.h

triangle->rect.w = triangle->rect.w - triangle->rect.x;

triangle->rect.y = triangle->rect.y - triangle->rect.h;

// Calculate max value for x

triangle->rect.w = triangle->sx1;

if (triangle->sx2 > triangle->rect.w)

{

triangle->rect.w = triangle->sx2;

}

if (triangle->sx3 > triangle->rect.w)

{

triangle->rect.w = triangle->sx3;

}

// Calculate min value for x

triangle->rect.x = triangle->sx1;

if (triangle->sx2 < triangle->rect.x)

{

triangle->rect.x = triangle->sx2;

}

if (triangle->sx3 < triangle->rect.x)

{

triangle->rect.x = triangle->sx3;

}

// Calculate max value for y

triangle->rect.y = triangle->sy1;

if (triangle->sy2 > triangle->rect.y)

{

triangle->rect.y = triangle->sy2;

}

if (triangle->sy3 > triangle->rect.y)

{

triangle->rect.y = triangle->sy3;

}

// Calculate min value for y

triangle->rect.h = triangle->sy1;

if (triangle->sy2 < triangle->rect.h)

{

triangle->rect.h = triangle->sy2;

}

if (triangle->sy3 < triangle->rect.h)

{

triangle->rect.h = triangle->sy3;

}

}

/*

* Fill the triangle on the surface with the triangle's color

*/

void fill_triangle(SDL_Surface *surface, triangle_t *triangle)

{

// TODO: Insert code that fills the triangle with the color specified in triangle->fillcolor.

// Hint: Draw the triangle with color TRIANGLE_PENCOLOR (this color can not

// occur in e.g. the teapot or the example triangles). Thus, if your

// approach to filling the triangle relies on looking for the edges of

// the triangle on the surface (via the GetPixel function), you will find those

// edges even if the triangle overlaps with a triangle that has already

// been drawn on the surface.

int i, j, k;

for (k = triangle->rect.h; k <= triangle->rect.h; k++)

{

for(i = triangle->rect.x; i <= triangle->rect.w; i++)

if (get_pixel(surface, i, k) == TRIANGLE_PENCOLOR)

{

break;

}

for (j = triangle->rect.y; j <= triangle->rect.h; j++)

if (get_pixel(surface, j, k) == TRIANGLE_PENCOLOR)

{

break;

}

draw_line(surface, i, k, j, k, triangle->fillcolor);

}

}

/*

* Draw a filled triangle on the given surface

*/

void draw_triangle(SDL_Surface *surface, triangle_t *triangle)

{

int isOK;

/* Scale. */

scale_triangle(triangle);

/* Translate. */

translate_triangle(triangle);

/* Determine bounding box */

calculate_triangle_bounding_box(triangle);

/* Sanity check that triangle is within surface boundaries. */

isOK = sanity_check_triangle(surface, triangle);

if (!isOK) {

print_triangle(triangle, "Triangle outside surface boundaries");

return;

}

/*

* TODO: Insert calls to draw_line to draw the triangle.

* Remember to use the on-surface coordinates (triangle->sx1, etc.)

*/

draw_line(surface, triangle->sx1, triangle->sy1, triangle->sx2, triangle->sy2, TRIANGLE_PENCOLOR);

draw_line(surface, triangle->sx2, triangle->sy2, triangle->sx3, triangle->sy3, TRIANGLE_PENCOLOR);

draw_line(surface, triangle->sx3, triangle->sy3, triangle->sx1, triangle->sy1, TRIANGLE_PENCOLOR);

/* Fill triangle */

fill_triangle(surface, triangle);

}

]]>

Code:

`#include <stdio.h>`

#include <stdlib.h>

#define N 5

int main()

{

int counter = 0, num = 0;

int memory[N];

while(scanf("%d", &num) != EOF){

if (counter == N){

for(int i = 0; i< N-1; i++){

memory[i] = memory[i+1];

}

memory[N-1] = num;

continue;

}

memory[counter] = num;

counter++;

}

for(int i = 0;i < counter; i++){

printf("%d ", memory[i]);

}

return 0;

}

My while loop wouldn't terminate when I enter -1 as value, it just keeps registering more and more numbers into the array even the -1 and never reaches the second part of the code(the printf).

the only solution I could think of is adding another if statement but I feel like it's supposed to be working without it.

Code:

`#include <stdio.h>`

#include <stdlib.h>

#define N 5

int main()

{

int counter = 0, num = 0;

int memory[N];

while(scanf("%d", &num) != EOF){

if(num == EOF){

break;

}

if (counter == N){

for(int i = 0; i< N-1; i++){

memory[i] = memory[i+1];

}

memory[N-1] = num;

continue;

}

memory[counter] = num;

counter++;

}

for(int i = 0;i < counter; i++){

printf("%d ", memory[i]);

}

return 0;

}

If it's not supposed to be working without the second if statement may I recieve a small explanation to why it is like this?

Thanks alot in advance! ]]>

Code:

`#include <stdio.h>`

#include <stdlib.h>

#include <assert.h>

#include <stdbool.h>

bool sorted_asc(void *data, size_t size, size_t datasize,

int (*compare)(const void *a, const void* b))

{

unsigned char *p = data;

for (int i = 0; i < size - 1 ; i++)

{

if(compare(p + i * datasize, p + ((i + 1) * datasize)) > 0)

return false;

}

return true;

}

bool sorted_desc(void *data, size_t size, size_t datasize,

int (*compare)(const void *a, const void* b))

{

unsigned char *p = data;

for (int i = 0; i < size - 1 ; i++)

{

if(compare(p + i * datasize, p + ((i + 1) * datasize)) < 0)

return false;

}

return true;

}

/* sorted - check if an array is sorted - either in descending or

* ascending order

*

* Possible use:

* assert(sorted(...));

* qsort(...)

* or

* assert(!sorted(...))

* qsort(...)

*/

bool sorted(void *data, size_t size, size_t datasize,

int (*compare)(const void *a, const void* b))

{

return sorted_asc(data, size, datasize, compare) ||

sorted_desc(data, size, datasize, compare);

}

int compare_int(const void *a, const void *b)

{

const int *i1 = a;

const int *i2 = b;

return *i1 - *i2;

}

int main(int argc, char **argv)

{

int a1[] = {1,2,3,4,5};

int a2[] = {5,4,3,2,1};

int a3[] = {3,2,1,4,5};

assert(sorted_asc(a1, 5, sizeof(int), compare_int));

assert(sorted_desc(a2, 5, sizeof(int), compare_int));

assert(!sorted(a3, 5, sizeof(int), compare_int));

assert(sorted(a1, 5, sizeof(int), compare_int));

assert(sorted(a2, 5, sizeof(int), compare_int));

printf("ALL TESTS PASSED\n");

return EXIT_SUCCESS;

}

Code:

`#include int main()`

{

float f = 0.1;

fwrite(&f, sizeof f, 1, stdout);

return 0;

}

here is my function:

Code:

`char *cat(char *dest, char *src)`

{

int j = strl(dest);

int i;

i = 0;

while (src[i])

{

dest[i + j] = src[i];

i++;

}

dest[i + j] = 0;

return (dest + i +j);

}

We paste the source at the first \0 of dest and then (this is were the problems start) we return the end of the pasted source so that we can keep on concatenating string.

for some reason if I do a printf of the returned string inside the function everything is fine but if I try to printf it outside the function I get a seg fault.

If you have any idea on what is wrong or can show me the way you solved this problem, please let me know.

Any help is very much appriciated, thanks for your time. ]]>

I settled on Qt because it is portable, well supported, has great documentation, I'm already using it (KDE) and will serve me as an excuse to start learning C++.

I was wondering what would be the best way to integrate the GUI into my existing program. Should I change my program into a library and link it into the GUI. Should I keep the CLI part? I'd like to but how difficult would it be to integrate.

I'm thinking the easiest way, specially considering the crosscompilation (I'm assuming Qt has a very complicated, unique way to do this) would be to compile my existing program as a library, exposing only certain general functions to communicate with the GUI which would collect and validate input, and produce feedback. I can simply gather input on the GUI code, pass it to a "collector" function that will populate my program's structures and validate the input.

This would be a very runabout way to do it though, and I'd be wasting opportunities to take advantage of the GUI to streamline my code. ]]>

Here is my code:

Code:

`void deleteFunc(struct Array *myArr, int x ){`

int i, size;

if(myArr -> length == 0)

{

printf("Value not found is in Array \n\n");

return ;

}

if(x >= 0 && x < myArr -> length){

for(i = x; i < myArr -> length -1; i++){

myArr -> A[i] = myArr -> A[i+1];

}

myArr -> length--;

printf("Array before deletion: ");

for(i = 0; i < size; i++){

printf("%d", myArr[i]);

}

printf("Array after deletion: ");

for(i = 0; i < size; i++){

printf("%d", myArr[i]);

}

}

or reload the browserDisable in this text fieldEditEdit in GingerEdit in Ginger×Enable Ginger

or reload the browserDisable in this text fieldEditEdit in GingerEdit in Ginger×Enable Ginger

or reload the browserDisable in this text fieldEditEdit in GingerEdit in Ginger×Enable Ginger

or reload the browserDisable in this text fieldEditEdit in GingerEdit in Ginger×Enable Ginger

or reload the browserDisable in this text fieldEditEdit in GingerEdit in Ginger× ]]>

Please, help to understand how could I pass string from C program into GNU Assembler one and operate it in GNU Assembler. Where could I read about it detailed explanation?

Thank you. ]]>

I have to fix the bug in code where function should be execute n times. Provided method is out of the times. Below code to fix

Code:

`void execute (void (*action)(), int nTimes) {`

for (int i = 0; i < nTimes; i += 1) {

action();

}

}

Code:

`static int i=0;`

void pusty(void)

{i++;}

typedef void (*action)(void);

void execute (action pusty, int nTimes) {

for (; i < nTimes;) {

pusty();

}

}

First time posting here. I am a bit of a noob, but have done my due diligence in trying to understand the behavior. Despite best efforts, I cannot understand or explain the behavior of the below code.

The program takes a text from stdin. The text is a simple stream of data comprised of numbers separated by newlines.

The goal of the program is to store these numbers into an array. The input size is unknown and therefore the program must dynamically allocate additional memory.

When realloc is ran inside main, there is no issue. When realloc is ran from a function call, the program errors with an eventual "realloc(): invalid next size".

To me, a noob, it appears the process is the same regardless of where realloc is called. There must be some fundamental concept I am missing. Could someone help enlighten my lack of understanding?

Again: If I leave the realloc portion uncommented in main and comment out the function call, it works as expected. If I comment out the realloc portion in main and uncomment the function call, it fails.

Below is the code (I will comment out the realloc in main):

Code:

`#include <stdio.h>`

#include <stdlib.h>

#include <string.h>

void push_int(int *array, int *counter, int *size, int *value){

if (*counter >= *size){

array = realloc(array, (*size + *counter)*sizeof(int)*2);

*size = (*size + *counter)*2;

}

array[*counter] = *value;

}

int main (int argc, char *argv[]){

char *line = NULL;

size_t len = 0;

ssize_t read;

char *tok = NULL;

int number = 0;

int size = 1;

int *array = malloc(sizeof(int));

int counter = 0;

if (stdin == NULL) {

exit(EXIT_FAILURE);

}

while ((read = getline(&line, &len, stdin)) != -1) {

tok = strtok(line, "\n");

number = atoi(tok);

//if (counter >= size){

// array = realloc(array, (size + counter)*sizeof(int)*2);

// size = (size + counter)*2;

//}

push_int(array, &counter, &size, &number);

//array[counter] = number;

counter ++;

}

}

I am compiling with: gcc -lm example.c -o example

Thank you ]]>

Code:

`#include <stdio.h>`

main int()

{

char name

prinft("What is your name?");

scanf("%c", &name);

printf("Hello %c, how are you?", name);

//then here i want to let the program only continue when the user has input the return key

//for example the user can say: I am doing fine "return key"

//and then the program continues

printf("I am happy you are doing well, goodbye");

return 0;

}

I habe 3 global variables that are arrays.Everything works fine but when i try to pass in main the 3 variables (make them local) i receive segmentation fault.Via debugging i ebserved that this happens just before entering the main functions.

When the 3 variables(

Any thoughts???

Thanks in advance. ]]>

It looks like it should work but I can't find the error at the end of main(): "error: expected expression before ')' token".

A search tells me it's likely a naming/type problem or syntax error. By the message, I'd assume it's in main() but I'm not seeing it. What am I missing??

Thanks.

Code:

`#include <stdio.h>`

#include <stdlib.h>

#include <math.h>

#define NUM_MIXTURE_PERCENTAGES 101

#define MOLAR_MASS_WATER 18.0153 /*Grams per mol*/

#define MOLAR_MASS_ETHANOL 46.0684 /*Grams per mol*/

/*Function Prototype*/

double convert(double abw_to_abv[], double abw); /*converts alcohol by weight %mass-%mass to %vol-%vol*/

double calc_abw(double mol_mass_e, double mol_mass_w, double mol_frac1, double mol_frac2);/*converts from mol% to %mass, mol fractions of x or mol fractions of y*/

int main()

{

int index = 0;

double x1 = 0; /*x1 is liquid molar fraction of ethanol*/

double x2 = 1-x1; /*x2 is liquid molar fraction of water*/

double boiler_abv = 0;

double abw = 0;

double abw_to_abv[9] = /*Array of E Croissants coefficients for conversion of alcohol by mass to alcohol by volume*/

{

-0.000039705486746795932, 1.2709666849144778, -0.40926819348115739, 2.0463351302912738,

-7.8964816507513707, 15.009692673927390, -15.765836469736477, 8.8142267038252680, -2.0695760421183493

};

for (int index = 0; index < NUM_MIXTURE_PERCENTAGES; index++)

{

abw = calc_abw(MOLAR_MASS_ETHANOL, MOLAR_MASS_WATER, x1, x2);

boiler_abv = convert(abw_to_abv, abw);

printf("\nfraction \tabw \tboiler_abv\n");

printf("%1.2f %1.2f %1.2f\n",

x1, abw, boiler_abv);

x1 += 0.01;

x2 += 0.01;

}

return();

}

/********************************FUNCTIONS******************************/

double calc_abw(double mm_e, double mm_w, double mf1, double mf2)

{

double result = 0;

result = mf1/(mf1 + mm_w/mm_e * mf2);

return (result);

}

/**********************************************************************/

/*Calculates alcohol by volume from alcohol by weight. Source: On the Conversion of Ethanol by Edwin Croissant 02-14-2016 */

/*Result between 0 and 1. Valid only for input between 0 and 1 and valid only at 20 degrees C*/

/*Initial polynomial y = a + b*x + c*x^2 + d*x^3 + f*x^4 + g*x^5 + h*x^6 + i*x^7 + j*x^8*/

double convert(double abw_to_abv[], double abw)

{

double abv = 0;

abv = abw_to_abv[0];

double abw_mult = abw;

for (size_t i = 1; i < sizeof(abw_to_abv) / sizeof(abw_to_abv[0]); ++i)

{

abv = abv + abw_to_abv[i] * abw_mult;

abw_mult *= abw;

}

return abv;

}