C++ :: GPA - Program Not Averaging Correctly?

Feb 7, 2015

My program isnt averaging correctly.

#include <iostream>
#include <string>
#include <iomanip>
#include <cstdlib>
using namespace std;

struct nameGPA {

[Code] .....

View 4 Replies


ADVERTISEMENT

C :: Averaging Array With A Function

Jun 1, 2013

I am trying to write a program that averages an array over every 5 values and stores this in a new array.

e.g. array = [1,2,3,4,5,6,7,8]
averaged_array = [ (1+2+3+4+5)/5, (2+3+4+5+6)/5, etc.]
= [3, 4, etc.]

ive been struggling a bit with passing arrays between functions but have most things working. the sum function works fine but the averaging one crashes when it tries to leave the function and return to main. so it prints the first 2 values of output at the end there and then dies. i have all the proper declarations and libraries.

also, how can i properly find the size of an integer array? like ive used sizeof but i dont know how long the output will be so can't calculate the required size to delcare it :

Code: int main()
{
int start_array[25] = {1,2,4,8,15,9,7,4,2,2,1,3,4,8,13,20,15,8,5,3,2,1,1,0,'$'};
int avg1[256];

[Code].....

View 14 Replies View Related

C++ :: Program Is Not Calculating Sums Correctly?

Jun 20, 2014

I am getting this weird error when calculating sums. Here is what my program does: it generates two random numbers and then randomly puts either an addition or subtraction sign between the two and then I am asked to enter what the correct answer for that question is. But on some of the subtraction questions when I give the answer it says I got it wrong after which the program shows the right answer. What's confusing is that the answer the computer calculates is the same that I entered. Anyways the complete program and a screenshot of the console window which shows the error are below. The function in question here is decAddSub at line 54. The error is shown in the first sum in the output window. It doesn't affect addition problems and only affects some of the subtraction problems.

#include<iostream>
#include<stdlib.h>
#include<time.h>
#include<random>

[Code].....

View 2 Replies View Related

C :: Blurring PPM Image By Averaging Components Of Color Within Certain Reach Of Specific Pixel

Mar 13, 2014

I'm trying to blur a ppm image by averaging the components of the color(r, g, b) within a certain reach of a specific pixel. This is a picture and description:

In this diagram, we are trying to compute the color for the pixel in the center (the red element). Its neighbors (within a reach of 4) are all of the green elements. The pixels outside of this 9x9 square are not considered in the blurring calculation for this pixel. To compute the color for the center pixel, average the red, green, and blue components (independently) of every pixel in the 9x9 square (including the pixel itself; the red element in this diagram).

I have some code, but it is not working correctly. The function "computed_color" on line 52 is shown in the next block of code.

Code:
#include <stdlib.h>
#include <stdio.h>
#include "blur_calculations.h"
#define NOT_ENOUGH_ARGS 2
#define MIN_ARGS 3
#define PICTURE_FILE 1
#define REACH_NUMBER 4
#define OUTPUT_FILE "blurred.ppm"
#define PPM_NUMBER "P3"
#define MAX_ROWS 500
#define MAX_COLS 500

void solve(FILE *in_picture, FILE *out_picture, char *argv[]) {

[Code] ....

Here is my code to calculate the average, which I use in my "solve" function:

Code:
#include "blur_calculations.h"
struct color create_color(int r, int g, int b) {
struct color colors;
colors.r = r;
colors.g = g;
colors.b = b;
return colors;

[Code] ....

View 4 Replies View Related

C++ :: Program Is Not Reading In Values From DAT File Correctly

May 20, 2013

We are supposed to split this lab into 3 files a .h and 2 .cpp's. The code I'll be talking about is going to be directed at the main.cpp file. Basically we are supposed to be able to enter as many classes into the program and when you are done the program is supposed to save the contents into a .dat file. This lab is working off of an older lab so I already have a .dat file I am trying to test with this program by reading it into my program when I first run it I prompt if the user wants to load previously entered courses and if they reply with a 'Y' the program will load the contents of the .dat file. Problem is right now when I load from the .dat file only the first class is correct. Every other class after that is just giberish. I've tried everything but can't get the restore part working. I also cannot get the save to .dat portion of this lab working either.Here is my code:

#include "Course.h"
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
using std::ios;

[code]....

View 1 Replies View Related

C :: Program Not Printing Mac Address Correctly From Ethernet Header

Jun 2, 2013

I have a procedure that prints the fields of ethernet frames and ip headers. I have an issue with the src mac addr being printed incorrectly and incompletely. The part in red is the trouble code.

Code:
1#include "sniffer.h"
2
3void print_headers(struct ethhdr * ethhdr, struct ip * iphdr){
4
5 char ipstr_src[INET_ADDRSTRLEN];
6 char ipstr_dst[INET_ADDRSTRLEN];
7 char macstr_dst[ETH_ALEN], macstr_src[ETH_ALEN];

[Code] ....

View 2 Replies View Related

C :: Program Won't Correctly Print Proper Totals On Screen

Oct 10, 2014

Why this program won't correctly print the proper totals on screen? See my code and the included screenshot.

Code:
#include <stdio.h>
int main(void) {
int total, first, second;
printf("Enter 2 numbers:

[Code] .....

View 4 Replies View Related

C/C++ :: Program To Emulate Shopping List - File Not Reading Correctly

Jan 28, 2015

I have written an program to emulate a shopping list. Everything works fine, until I try to read in from a file. Here is my read and write code (the write code looks like it is working ok, but I figured I would include it just in case), and an example of what happens to a list that has been written, then read back in.

I took this list, and wrote it to a file, then read that file back in. The .txt file looks like everything was written out properly, but when I try to read that exact same list back in, this is what I get:

View 10 Replies View Related

C :: How To Typecast Correctly

Feb 17, 2015

so i have this problem with my code ( not running)i want to read ( 2 double numbers x,y) and ( one integer z) then calculate reminder of x and y ( after they both converted to integer) and the formula : x^2 + y^2 + z^2and : x^z + y^z

here is what i came up with :

Code:
// compute.c

#include<stdio.h>
#include<math.h>
int main(void)
{

[Code]....

it also says in q : be sure to test the user input to make sure x,y,z are positive . any negative or zero should not be accepted and must print error msg ==> do i have to have an if statement here ? or the while loop is enough ?

View 9 Replies View Related

C :: How To Use Pow Function Correctly

Jan 23, 2015

I'm working on a school assignment that asks us to make code calculating the quadratic formula.

Code:

#include <stdio.h>
#include <math.h>
int main(void) {
double a = 0;
double b = 0;
double c = 0;
double discriminant = 0;
double solution = 0;
double solution2= 0;
}

[code]....

View 4 Replies View Related

C++ :: Board Not Printing Correctly

Jul 19, 2013

I'm writing a version of the classic Snake game. However, my board is not printing correctly. The right hand border is in the incorrect location. Also, when I randomly generate where the food ('X') is located, it only generates on the edges of the boundaries.

#include <iostream>
#include <cstdlib>
using namespace std;
const int ROWS = 21;
constint COLS = 61;

[code].....

View 5 Replies View Related

C++ :: CPP File Not Opening Correctly?

Nov 2, 2014

When I try to open the cpp source in Visual Studio, it just opens a blank notepad window. The cpp file says its 4 kb and there was code in it.

View 5 Replies View Related

C++ :: Calculations Are Done Correctly But Showing Zero

Nov 26, 2013

#include <iostream>
#include <cstdio>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
const int SZ = 55;

[Code] ....

View 1 Replies View Related

C++ :: Variables Not Passing Correctly

Mar 10, 2013

Well I have my program running and the Variables are not passing correctly and the return statements are not returning correctly. Here is the parts that are not working.

#include <iostream>
#include "fight.h"
#include <time.h>
#include "player.h"
#include "stdlib.h"
#include <cstdlib>
using namespace std;

combat A;
combat::combat(void)

[Code] ....

View 11 Replies View Related

C/C++ :: Do-while Loops Not Working Correctly

Oct 28, 2014

I have an assignment for class .. It works, the do-while loop isn't working correctly. Once I am doing inputting information for any employee It should ask to continue. It doesn't, It skips that loop and prompts to enter the type of employee again.

#include <iostream>
#include <iomanip>
using namespace std;
int main( ) {
char empInput;
char continueResponse;

[code].....

View 3 Replies View Related

C/C++ :: Won't Delete Entries Correctly

Mar 5, 2015

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

[Code]....

It compiles just fine and it will add new entries and print the entries just fine but when I go to delete an entry it will delete it but it will mess up the one before it.

View 8 Replies View Related

C++ :: Object Data Not Updating Correctly

Dec 14, 2013

I am writing a program that manages a group of tool bins. This group is handled as an object that is an array of two element structures called InvBin. I initialize the bins with data from a file which contains the descriptions and initial quantities. I also have functions to add or subtract items from a bin and a function to display a report of the description and quantity of all of the bins.

The add and remove functions work correctly based on the cout statement in the functions, however when I display the report, it displays the initial quantity instead of the new quantity. In addition, when I use the add and remove functions again on the same bin, they use the initial quantity.

These are the add and remove functions and the report function from the main program.

Code:
//Adds an item to a bin
void addItem(HANDLE screen, BinManager tools, int &count) {
int binNum;
int addNum;
system("cls");

[Code].....

View 2 Replies View Related

C :: How To Correctly Multiply With Negative Numbers

Jul 24, 2013

I've been working on this program to create a simple desk calculator for a school assignment, and I managed to finish. All we had to do was add, subtract, multiply, and divide positive integers - and I was able to do that just fine. This program got me thinking though, because I do not know how to write commands to multiply/divide negative numbers.

In fact, when I divide a number like 21 by 4, it comes out to 5 because I don't know how to allow it to compute remainders (which wasn't a requirement for my program). This intrigued me so I've been trying to figure it out for the last few days but to no avail. Here's my code:

Code: void flush_buffer(){
int ch;
while ((ch = getchar()) != '
' && ch != EOF);

[Code]....

And just know that my code works perfectly fine, I'm not here for troubleshooting it. I just want to know what I can change to allow negative values to be correctly computed.

View 12 Replies View Related

C# :: Not Loading Dynamic Array (128,8) Correctly

Feb 14, 2013

Code:
public void dam_data_setup() {
// fill list
damgtype.Add( den1);
damgtype.Add( den2);
damgtype.Add( da1);
damgtype.Add( da2);
damgtype.Add( db1);
damgtype.Add( db2);

[Code] .....

This is a genetics program and is to parse the source array and write all possible combinations to a new array. All sections but dilute work correctly. For some reason the dilute's Boolean is not testing true when it should. This is causing data corruption.

View 3 Replies View Related

C :: Why Can't Get Info From Text File Correctly

Dec 21, 2013

the info gotten from the text file are all wrong, i can't find any problem with my code,

Code:

#include<stdio.h>
FILE *f;
typedef struct prd {
char name[30];
char code[30];
char idt[30];
float price;

[code]....

View 7 Replies View Related

C++ :: Constant Char Not Being Interpreted Correctly

Oct 23, 2013

I am trying to use libXl to output text from a C++ program to an Excel file. The problem is coming with this library function:

bool writeStr(int row, int col, const wchar_t* value, Format* format = 0)

Writes a string into cell with specified format. If format equals 0 then format is ignored. String is copied internally and can be destroyed after call this method. Returns false if error occurs. Get error info with Book::errorMessage().

If I give input as a string literal like "Hello World" it is displayed correctly. However, if I try to give input as a variable of type const char*, it displays garbage.

Following is my code. MyCompany::company is a QString.

const char* companyName = MyCompany::company.toStdString().c_str();
sheet->writeStr(4, 0, companyName, companyFormat);

View 3 Replies View Related

C++ ::  Adding Fractions - How To Align Correctly

Mar 18, 2014

Ii need an output like this...

#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <ctype.h>
#include <locale>
using namespace std;

int EquationNum (int Num1, int Den1, int Num2, int Den2);

[Code] ....

View 9 Replies View Related

C++ :: File I/O Not Recognizing Contents Correctly

Nov 17, 2014

I have a program that's not doing what I want it to do. This is the assignment:

The nth term of the sequence of triangle numbers is given by, tn = 1/2 n (n+1); so the first ten triangle numbers are:

1, 3, 6, 10, 15, 21, 28, 36, 45, 55,...

By converting each letter in a word to a number corresponding to its alphabetical position and adding these values we form a word value. For example, the word value for SKY is 19 + 11 + 25 = 55 = t10. If the word value is a triangle number then we shall call the word a triangle word.

the 'words.txt' has a bunch of words in this format:

"ABSTRACT"
"YOUTH"

there's about 1000 words in that format.

And for any triangle words I find, I put it into another text file which I called triangle.txt.

Now for my program, it pulls the 'words.txt' file just fine but it doesn't recognize any of the words in the file as triangle words.

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <cmath>
#include <cstdlib>
int getCharValue(char a);
double sum1(std::string name);

[code]....

View 2 Replies View Related

C++ :: Implementing Collision Detection Correctly

Sep 10, 2014

I'm making a 2D Terraria-like (side-scrolling RPG with destroyable blocks) game with SFML 2.1, and I have no clue how to correctly implement collision detection. Meaning that I have been successful at making it work, but it's not clean and definitely not dynamic.

By example, I mean design wise. What parameters should collision functions take? Where should these functions be located? The same 2 questions for collision reaction. Since I have multiple things that need to collide with each other (player with enemy, player with items, enemy with items, enemy with enemy, player with player, etc...), and the world has to be treated differently, things get complicated. There is no block class, just a vertex array, so collision with the world is based purely on coordinates taken from that array in the world class, which, considering it's the world and not something like a player or an enemy, doesn't inherit from the same base class that they do.

View 7 Replies View Related

C++ ::  how To Correctly Initialize Instances Of A Class

Feb 1, 2015

I am struggling to understand what the correct way to initialize instances of a class for object orient programming is.

If I am not mistaken, there are 2 methods to initialize an instance "instance_name" of CLASS1 by calling the constructor:

(1) CLASS1 instance_name(argument);
(2) CLASS1 instance_name=CLASS1(argument);

As it turns out, I cannot use method (1) when I try to initialize a private instance inside another class. But I can use it in the main() code. I hope the code below will explain what I mean. Why only method (2) will work inside another class? Or is there another fundamental mistake I am making?

(BTW: I used CodeBlocks 13.12 with GNU GCC compiler for this example)

#include <iostream>
using namespace std;
class CLASS1{

[Code].....

View 6 Replies View Related

C/C++ :: Maze Solver Is Not Backtracking Correctly?

Mar 15, 2014

I'm working on a homework problem where we are given a class of functions to traverse a maze and must use those functions to recursively find the end.

The problem I'm having is when the function backtracks it doesn't reset the whole path it came from.

Here is my function that I have created

void solveMaze(Maze M, int path[][MAX])
{
// << overwritten to print the maze out
cout<<M;

[Code].....

View 7 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved