C :: Pointers Array De-allocation And Input In A Line

May 24, 2013

I have three questions concerning the below code I have written, which is expected to do basically as the attached diagram illustrates:

Code:
#include <stdio.h>
#include <stdlib.h>
double getAverageGrade(int studentGrades[] , int size);
int* getStudentInfo(int * numCourses);
int main()

[Code] ...

(1) How may I display the averages to 2 decimal places, namely why doesn't %.2lf work? The program gives 89.00 for 89 and 90, for instance.
(2) How may I read the number of courses and the relevant grades in a row for each student?
(3) How may I free (de-allocate) my array of pointers, namely grades? Does it suffice to use free(grades)?

View 12 Replies


ADVERTISEMENT

C :: Dynamic Memory Allocation - Resize Array Of Pointers

May 23, 2013

Suppose I wished to reallocate memory (resize) an array of pointers. Why does the following not work?(The program runs, yet yields a faulty segmentation error message. Why?):

Code: char **ptrarr = (char**) malloc(sizeof(char*))

Code: ptrarr = (char**) realloc(ptrarr, (capacity) * sizeof(char*));

View 14 Replies View Related

C :: Copying File Line By Line Using Dynamic Memory Allocation

Jul 15, 2013

I need to read lines from one file and copy them line by line into another file using dynamic memory allocation. It compiles but gives me a seg fault. Why/How?

Code:
int main(){
FILE *fp1;
FILE *fp2;
FILE *i;
fp1=fopen("file1","r");
fp2=fopen("file3","w+");

[Code] ....

View 2 Replies View Related

C++ :: Dynamic Allocation Of Two Dimensional Array With Its Size As Input

Nov 8, 2013

I'm now working on a class to handle matrices and matrix operations. I dynamically allocate a two dimensional array with its size as an input. My problem is that i need to resize the matrix structure (to fit to the size of a product of two matrices or to adjust the size after deleting a row or column for example). I make the resizing by deleting the allocated 2d array after saving its first element address to another pointer to pointer, after saving the useful data in an other array, then i use new[] operator to create a new 2d array using the same address of the old deleted array.

void My_matrix ::delete_row(int a) {
int i,j;
int buffer_row_index = -1; //note that our work is zero based, as we acess the private member of an object of the same class we are working on and we access to the array as it is (zero based) so if this variable is -1, it will be 0 when it enters for the first time and saves the data in the right position.
My_matrix buffer(number_of_rows-1,number_of_columns);

[Code] ....

I do this in a way that i think is unsafe. This code is sometimes unstable and gives an unhandled exception whose reason is unknown. I need to know whether this way is good enough or if there any other better ways.

View 5 Replies View Related

C :: Dynamic Allocation Of String Pointers

Mar 12, 2014

The snippet below (or similar) compiles and runs OK but I am using Visual Studio C++ compiler. Are the lines where .nameFirst and .nameLast assigned kosher in ANSI C?

Also I am concerned about the memory allocation for these string constants. Does the runtime system put them on the heap? It doesn't seem that they are really constants since they are not defined before runtime.

Code:

#include "stdlib.h"
typedef struct
{
unsigned id;
char* nameFirst;
char* nameLast;
} myList;

[Code]...

View 4 Replies View Related

C :: Double Pointers And Dynamic Memory Allocation?

Nov 16, 2013

we are currently covering double pointers and memory allocation. Currently getScrabbleWords is not working. when I compile with commented code (Main() works fine) I get a segmentation fault.

This is the purpose of getScrabbleWords:

char **getScrabbleWords(char **allWords, char letters[]):

This function takes an array of char* values (i.e. strings) representing all the words read from wordlist.txt. Each of these words is tested by callingcanWeMakeIt as a helper function, and pointers to the words that can be made are put into an array, myWords. Note, copies of the words are not made! In order to indicate the end of myWords, we terminate with a NULL pointer. Thus, if N words can be made from letters then myWords should have length N+1.

View 6 Replies View Related

C/C++ :: Pointers - Scope Of Dynamic Allocation And Delete

Apr 13, 2014

I have the following code here. My questions are:

1. Pointers can be used as pass by reference. When I dynamically allocated memory for array[50] in the run function, does that mean I am changing the size of the pArray in main as well? Or does the scope of array[50] ends with the function run? if so, should I do a delete [] Array inside the run function?

2. When I do delete[] pArray in main, what does it delete? memory for array[50]? or array[100]?

#include <iostream>
using namespace std;
void run(int* Array, int& s) {
s = 50;
Array = new int[s];

[Code] ....

View 10 Replies View Related

C++ :: How To Use Dynamic Memory Allocation And Pointers To Iterate Through The Arrays

Dec 21, 2014

I need to use dynamic memory allocation and use pointers to iterate through the arrays that I have already in this program. I am lost, nothing I do works and where to use the pointers. I am just looking for a push in the right direction so I can finish this project and how I can implement pointers in my program.

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

[Code]...

View 1 Replies View Related

C++ :: Input Elements Of Two Dimensional Array In Same Line

Nov 4, 2013

I want to input the elements of a two dimensional array in the same line . So while giving input . when i press enter . it should remain on the same line ? HOW to do that ?

View 1 Replies View Related

C :: Make Array Stop Reading From Input When It Hits A New Line?

Nov 25, 2013

I want the numbers to be put into the array until it hits a new line. Maximum number of numbers is 1000. What can I replace that while loop with to stop it from reading further into the input file?

Code:

while (input != '
'){
for(i=0; i<1000; i++)
fscanf(ifp, "%lf", &auctions_prices[i]);

View 2 Replies View Related

C :: Create Array Of Pointers To Pointers Which Will Point To Array Of Pointers

Feb 28, 2014

I'm trying to create an array of pointers to pointers which will point to array of pointers (to strings) I tried

Code:

int i;
char *string[]={
"my name is dave",
"we like to dance together",
"sunny day",
"hello",

[code]...

the app keeps crashing , I don't know how to make the array-elements to point to another array-elements..

View 4 Replies View Related

C++ ::  Dynamic Allocation 2D Array

Nov 14, 2013

I need to confirm that this problem cannot be solved without a pointer. Namely I need to read the rows and columns number from the user cin >> m, n and then use to declare an array int A[m][n];

However as m and n are not constants I am not able to do that. Is there a workaround? The following is the solution I came with BUT using a pointers which should be not the case.

// solution with using pointers as "int A[m][n]" does not work for me!!!
void TwoDimensionalArrayFunc(){
int m = 0;
int n = 0;

// instruct the users to enter array dimensions
cout << "Please insert value for m:";
cin >> m;

[Code] ....

View 6 Replies View Related

C++ :: Dynamic Allocation From Array?

Dec 26, 2012

I need to write 2 functions:

1. MyMalloc
2. MyFree
___________________________

I have a 1000 bytes global array (which did not dynamic allocated).

I need to make "dynamic allocation" from this array.

For example - MyMalloc(50) ---> The program will allocate 50 bytes OF THE ARRAY'S SIZE.
------
MyFree(pointer) ---> I need to check if the pointer is in the array and free the space.

It should be managed by blocks. The array should also contain the manage variables (for me).

View 19 Replies View Related

C++ :: Memory Allocation To Array

Jul 13, 2014

1) int *a=new int[10]
2) int a[10]

What are the exact differences in these two types of methods of allocating memory for an array ? When does 1st method is useful and when does 2nd ?I also read somewhere that in Ist method memory is allocated from heap but i don't know from where memory is allocated in 2nd method and what difference these memory allocations causes.

View 3 Replies View Related

C++ :: Memory Allocation For Array?

Dec 18, 2013

Here is my code:

Code:

class Base {
};
class Derived1 : public Base {
};
class Derived2 : public Base {
} class Bar {
public:
void SomeFunc();

[code].....

MSVC2010 throws out compiler error which says:

Code:

no operator found which takes a right-hand operand of type 'Derived *' (or there is no acceptable conversion)

What I don't understand is why? The pointer is an address of 0 element of an array. So what is the problem? I can eliminate the error by using double pointer but it will be an overkill.

View 8 Replies View Related

C++ :: Dynamic Allocation For String Array

Jul 27, 2013

I coded a program that takes some strings and lexicographically orders the strings and its substrings. I have used dynamic memory allocation technique and its working fine for all strings without consecutive same alphabets.I use a list in which a string is placed in its exact position by moving the others right.

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

[code]....

View 4 Replies View Related

C++ :: Dynamic Array Allocation Program

Dec 1, 2014

I am trying to read from a file the names, id numbers, and 4 grades of 5 stduents. Here is the code:

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const int GRADES = 4;
const int STUDENTS = 5;
struct StudentInfo {
[Code] .....

Here is the txt file:

Amy Adams
10111
97 86 78 95
Ben Barr
20222
89 81 73 87
Carla Carr
30333
79 71 63 77
Don Davis
40444
69 62 58 67
Edna Eaton
50555
63 51 62 48

When I run the program this is the output:
Amy Adams
10111
97
86
78
95

-842150451
-6.27744e+066
-6.27744e+066
-6.27744e+066
-6.27744e+066
and so on .....

Press any key to continue . . .

As you can see the program is reading the first students information and outputting that fine, but the rest of the students have bad values for output. I'm guessing it's something to do with the pointer, but I really can't figure it out, why it won't read all of the students info?

View 4 Replies View Related

C++ :: Allocation / Deallocation Of Array Within Struct

Feb 27, 2013

Okay so I have a question. Lets say I want to create arrays of exact size for abstract data type members. For example:

struct student {
char name[10];
int idNumber;
};

My question is how to declare this array as null so that later i can give it an exact size to match the length of the name. if it were not a part of the struct i would imagine that the code would look something like this:

char * name = NULL;
name = new char[10];

View 4 Replies View Related

C++ :: Dynamic Array Allocation For Test Scores

Nov 30, 2014

I was doing a side programming challenge in my workbook that asked me to dynamically allocate and array for test scores. So far I have an array that will accept an integer to allocate the amount of test scores and then will accept integers to populate the array, however when I try to cout the contents of the array the numbers are absurdly different than what was put in. Here's my code:

#include "stdafx.h"
#include <iostream>
using namespace std;
void main() {
cout << "How many test scores?" << endl;
int scores(0);

[Code] ....

And this is the output screen:
How many test scores?
4
Enter test score 1:
22
Enter test score 2:
33
Enter test score 3:
44
Enter test score 4:
55
-33686019
18472
55656634
201345063
Press any key to continue . . .

Why am I getting these crazy numbers? I've looked back and forth from examples in my book and it doesn't look like I'm doing anything wrong.

View 5 Replies View Related

C :: Dynamic Memory Allocation - User To Choose Size Of Array

Dec 2, 2013

Dynamic memory allocation in array in c programming. I am trying to make the user to choose the size of array they want to engage in the game.

However, i have remove the global variable which contribute the error to my code previously. Now I assigned all the arr individually but not using the global variable. However, i still not get the desired board i want. i still keep getting 9x9 array board.

And i also need limit the board size only from 4 to 9. And how do i do that.

Code:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#include <malloc.h>
#include <ctype.h>
#include <stdbool.h>

[Code] .....

View 3 Replies View Related

C/C++ :: Dynamic Allocation Of Array And Increase Its Size Every Time New Integer Inputted By User

Aug 29, 2014

I'm a little lost with this program. The idea is to dynamically allocate an array and increase its size every time a new integer is inputted by the user. I believe it is a memory leak but as we have just started learning this I'm not sure how to recognise it. Sometimes I can input as many integers as I want other times 2 or 3 before it crashes. When I can input enough values i exit the loop and send it to the sort function and mean calculator function, all works fine there except the last number inputted becomes this huge value not hexadecimal though... As such I'm at a loss as what to look at next, so here you go:

#include <iostream>
using namespace std;
void SelectSort(int [], int);
float MeanCalc(int [], int);
float MedianCalc(int* [], int);

[Code] .....

View 14 Replies View Related

C :: How To Read A 2 Line Input

Mar 13, 2013

I want to be able to read 2 line input, then safe them in separate arrays.

line1 input=array1
line2=array2

View 7 Replies View Related

C/C++ :: How To Get Input From User At Same Line

Dec 8, 2014

I am trying to get my input from user in the same line with certain width between but my output is not in the same line. my third variable which is fruit[i] is shown one line below . How I can fix this ?

#include <iostream>
using namespace std;
int main () {
int staff[3] , fruit[3];

[Code] ....

View 5 Replies View Related

C++ :: How To Input Two Variables From Same Line

May 27, 2014

How to input two variables in the same line? Like when i type 12 and press enter, 1 must be assigned to a, and 2 must be assigned to b. I want it to be entered on the same line, and press enter only once. How do i do that?

View 5 Replies View Related

C :: Have One Line Of Input Trigger Actions?

Nov 26, 2013

I want the phrase "BUY TICKET" to trigger something, i'm thinking an if statement, and then assign the number after the phrase to another variable. There will also be other commands entered from the input file such as "BUY RAFFLE' "TOTAL REVENUE" on the following lines. Sounds like I need to compare a string(?) to a constant phrase, and if they match have it run the specific code?

Code:

//scan in from file
fscanf(ifp, "%c", &input);
if (input == 'BUY TICKET'){
//Run this
}

How to do it using the tools in C.

View 4 Replies View Related

C :: Why Not The First Line At The Standard Input Read

Mar 21, 2013

Here's my code, it's for a poker hand evaluator

input:
'ad 2d 3d 4d 5d...
...4s 5s 6s 7s 8s'

I think it should go through all the 10 crads and disply the name of each card, but it seems to only do it for the second hand

Code:
include <stdio.h>
#include <ctype.h>
int main( void ) {
char inputtedhand[64];
int z=0, counter=0;
int index;
int i=0,m=0,n=0;

[Code].....

View 3 Replies View Related







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