GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 28-Oct-2009, 14:16
sl02ggp sl02ggp is offline
New Member
 
Join Date: Oct 2009
Posts: 25
sl02ggp is on a distinguished road

How do I add arrays into my program?


Trying to make this setup for my arrays:
Print Output Format: Print total resistance followed by the values of each resistor.

For series: (Example)
Total series resistance is 2.123 kOhm for 3 resistors:
1000 kOhm, 1000 kOhm, 123 kOhm

For parallel: (Example)
Total series resistance is 500 Ohm for 2 resistors:
1000 Ohm, 1000 Ohm



Its not compiling need help!...i think my parallel code is incorrect along with my arrays.



CPP / C++ / C Code:


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


int main()
{
	char type;																					
	//declare a character type variable
	double total_resistance=0;																	
	//initialize double total_resistance=0
	double resistor_value=0;																	
	//initialize double resistor_value=0
	int number_of_resistors_connected=0;														
	//declare number_of_resistors_connected=0
	
	


	double *r; //A pointer to hold the address of your dynamically-allocated "array"
    while (true) 
	{
        cout << "Please input 's' for series OR 'p' for resistors parallel"
            << endl;
        cin >> type;
        cout << "Please enter the number of resistors connected" << endl;
        cin >> number_of_resistors_connected;
        if (!cin || (number_of_resistors_connected <= 0)) {
            cout << "Invalid input. Must be a positive integer." << endl;
           
        }
        r = new double[number_of_resistors_connected];// Storage to hold the resistors' values
        if (type == 's') {
             total_resistance = 0.0;
            for (int i = 0; i < number_of_resistors_connected; i++) {
                cout << "Input value of  series resistor number " << i + 1 << endl;
                cin >> r[i];
                if (!cin) {
                    cout << "Invalid input." << endl;
                    
                }
                total_resistance += r[i];
            }
        }
		if (type == 'p') {
           total_resistance = 0.0;
            for (int i = 0; i < number_of_resistors_connected; i++) {
                cout << "Input value of  parallel resistor number " << i + 1 << endl;
                cin >> r[i];
                if (!cin) {
                    cout << "Invalid input." << endl;
                    
                }
                total_resistance += r[i];
            }
        }
	}
	{
	int[6];
	array = {1, 2, 3, 4, 5, 6};
	int length = sizeof(array) / sizeof(int);

	for(int i = 0; i < length; i++) {
	cout << "Resistor: " << length[i] << endl;
	}

	if (total_resistance<1000)																	//if total_resistance<1000
	{
		int x=0;																				//declare x=0
		x=total_resistance;																		
		//declare c = total resistance
		cout<<"The total resistance is less than 1000 and its value is "<<x<<" Ohm"<<endl;		//prompt message on screen
	}
	else																						//false statement
	{
		total_resistance=total_resistance/1000;													
		//total_resistance=total_resistance/1000
		cout<<fixed<<showpoint<<setprecision(3);
		cout<<"The total resistance is greater than 1000 and its value is "<<total_resistance<<" kOhm"<<endl;    
		//prompt message on screen
	}


	delete [] r;
	return 0;																					
	//indicate program ended successfully


}

Last edited by admin : 29-Oct-2009 at 03:44. Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
  #2  
Old 28-Oct-2009, 14:29
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 713
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: How do I add arrays into my program?!?!


Quote:
Originally Posted by sl02ggp
int[6];
array = {1, 2, 3, 4, 5, 6};
Maybe you were looking to create an array here called "array"?:
CPP / C++ / C Code:
int array[6] = {1, 2, 3, 4, 5, 6};

If you are having compilation errors, post the error message and the line of code that it is referencing and we can help.
  #3  
Old 28-Oct-2009, 14:40
sl02ggp sl02ggp is offline
New Member
 
Join Date: Oct 2009
Posts: 25
sl02ggp is on a distinguished road

Re: How do I add arrays into my program?!?!


thanks! i accidentally put that statement under the int.....fixed that.

also these are the errors that are showing up in my code:
Line (65) : error C2109: subscript requires array or pointer type

Line (71) : warning C4244: '=' : conversion from 'double' to 'int', possible loss of data

Line (92) : fatal error C1075: end of file found before the left brace '{' at '.\Resistor Calculation Using Arrays.cpp(9)' was matched
  #4  
Old 28-Oct-2009, 15:11
sl02ggp sl02ggp is offline
New Member
 
Join Date: Oct 2009
Posts: 25
sl02ggp is on a distinguished road

Re: How do I add arrays into my program?!?!


fixed it my errors, compiles but doesn't run the way i want it to.

k...here is what im trying to do..never really gave a proper description of my program

Trying to write a program to calculate the resistance connected either in series or parallel. Using arrays in solving the problem/

Prompt the user to select series or parallel.
Prompt the user to enter the number of resistors connected.
Read/get the value of each of the resistors
calculate the resistance.
Output the resistance according to the following:
Print Output Format: Print total resistance followed by the values of each resistor.

For series: (Example)
Total series resistance is 2.123 kOhm for 3 resistors:
1000 kOhm, 1000 kOhm, 123 kOhm

For parallel: (Example)
Total series resistance is 500 Ohm for 2 resistors:
1000 Ohm, 1000 Ohm

Avoid division by 0


CPP / C++ / C Code:



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


int main()
{
	char type;																					
	//declare a character type variable
	double total_resistance=0;																	
	//initialize double total_resistance=0
	double resistor_value=0;																	
	//initialize double resistor_value=0
	int number_of_resistors_connected=0;														
	//declare number_of_resistors_connected=0
	
	


	double *r; //A pointer to hold the address of your dynamically-allocated "array"
    while (true) 
	{
        cout << "Please input 's' for series OR 'p' for resistors parallel"
            << endl;
        cin >> type;
        cout << "Please enter the number of resistors connected" << endl;
        cin >> number_of_resistors_connected;
        if (!cin || (number_of_resistors_connected <= 0)) {
            cout << "Invalid input. Must be a positive integer." << endl;
           
        }
        r = new double[number_of_resistors_connected];// Storage to hold the resistors' values
        if (type == 's') {
             total_resistance = 0.0;
            for (int i = 0; i < number_of_resistors_connected; i++) {
                cout << "Input value of  series resistor number " << i + 1 << endl;
                cin >> r[i];
                if (!cin) {
                    cout << "Invalid input." << endl;
                    
                }
                total_resistance += r[i];
            }
        }
		if (type == 'p') {
           total_resistance = 0.0;
            for (int i = 0; i < number_of_resistors_connected; i++) {
                cout << "Input value of  parallel resistor number " << i + 1 << endl;
                cin >> r[i];
                if (!cin) {
                    cout << "Invalid input." << endl;
                    
                }
                total_resistance += r[i];
            }
        }
	}
	{
	int array [] = {1, 2, 3, 4, 5, 6};
	int length = sizeof(array) / sizeof(int);

	for(int i = 0; i < length; i++) {
	cout << "Resistor: " << length<< endl;
	}

	if (total_resistance<1000)																	//if total_resistance<1000
	{
		double x=0;																				//declare x=0
		x=total_resistance;																		
		//declare c = total resistance
		cout<<"The total resistance is less than 1000 and its value is "<<x<<" Ohm"<<endl;		//prompt message on screen
	}
	else																						//false statement
	{
		total_resistance=total_resistance/1000;													
		//total_resistance=total_resistance/1000
		cout<<fixed<<showpoint<<setprecision(3);
		cout<<"The total resistance is greater than 1000 and its value is "<<total_resistance<<" kOhm"<<endl;    
		//prompt message on screen
	}


	delete [] r;
	return 0;																					
	//indicate program ended successfully


}
}

Last edited by admin : 29-Oct-2009 at 03:46. Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
  #5  
Old 28-Oct-2009, 15:43
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 713
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: How do I add arrays into my program?!?!


Reposted your code using CPP tags:
CPP / C++ / C Code:
#include <iostream>
#include <iomanip>
using namespace std;


int main()
{
	char type;																					
	//declare a character type variable
	double total_resistance=0;																	
	//initialize double total_resistance=0
	double resistor_value=0;																	
	//initialize double resistor_value=0
	int number_of_resistors_connected=0;														
	//declare number_of_resistors_connected=0
	
	


	double *r; //A pointer to hold the address of your dynamically-allocated "array"
    while (true) 
	{
        cout << "Please input 's' for series OR 'p' for resistors parallel"
            << endl;
        cin >> type;
        cout << "Please enter the number of resistors connected" << endl;
        cin >> number_of_resistors_connected;
        if (!cin || (number_of_resistors_connected <= 0)) {
            cout << "Invalid input. Must be a positive integer." << endl;
           
        }
        r = new double[number_of_resistors_connected];// Storage to hold the resistors' values
        if (type == 's') {
             total_resistance = 0.0;
            for (int i = 0; i < number_of_resistors_connected; i++) {
                cout << "Input value of  series resistor number " << i + 1 << endl;
                cin >> r[i];
                if (!cin) {
                    cout << "Invalid input." << endl;
                    
                }
                total_resistance += r[i];
            }
        }
		if (type == 'p') {
           total_resistance = 0.0;
            for (int i = 0; i < number_of_resistors_connected; i++) {
                cout << "Input value of  parallel resistor number " << i + 1 << endl;
                cin >> r[i];
                if (!cin) {
                    cout << "Invalid input." << endl;
                    
                }
                total_resistance += r[i];
            }
        }
	}
	{
	int array [] = {1, 2, 3, 4, 5, 6};
	int length = sizeof(array) / sizeof(int);

	for(int i = 0; i < length; i++) {
	cout << "Resistor: " << length<< endl;
	}

	if (total_resistance<1000)																	//if total_resistance<1000
	{
		double x=0;																				//declare x=0
		x=total_resistance;																		
		//declare c = total resistance
		cout<<"The total resistance is less than 1000 and its value is "<<x<<" Ohm"<<endl;		//prompt message on screen
	}
	else																						//false statement
	{
		total_resistance=total_resistance/1000;													
		//total_resistance=total_resistance/1000
		cout<<fixed<<showpoint<<setprecision(3);
		cout<<"The total resistance is greater than 1000 and its value is "<<total_resistance<<" kOhm"<<endl;    
		//prompt message on screen
	}


	delete [] r;
	return 0;																					
	//indicate program ended successfully


}
}
  #6  
Old 28-Oct-2009, 15:52
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 713
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: How do I add arrays into my program?!?!


One problem that I see immediately is that you have a never-ending loop where you ask for input and put it into arrays.

Try creating the program to run without a loop first so that each time you run the program, it runs through the process of collecting user input and displaying output just once. In other words, take out the while(true) loop and see what happens.
  #7  
Old 28-Oct-2009, 16:29
sl02ggp sl02ggp is offline
New Member
 
Join Date: Oct 2009
Posts: 25
sl02ggp is on a distinguished road

Re: How do I add arrays into my program?!?!


yes that was the issue! the while (true)

now trying to get the format output correct:

"Print Output Format: Print total resistance followed by the values of each resistor."

For series: (Example)
Total series resistance is 2.123 kOhm for 3 resistors:
1000 kOhm, 1000 kOhm, 123 kOhm

For parallel: (Example)
Total series resistance is 500 Ohm for 2 resistors:
1000 Ohm, 1000 Ohm

this is what shows up now:
  #8  
Old 28-Oct-2009, 18:14
sl02ggp sl02ggp is offline
New Member
 
Join Date: Oct 2009
Posts: 25
sl02ggp is on a distinguished road

Re: How do I add arrays into my program?!?!


can anyone assist me with this? please? on why the resistor values aren't printing like I stated above and what i have to do to modify my code to print output this way??
  #9  
Old 28-Oct-2009, 21:03
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 803
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: How do I add arrays into my program?!?!


They aren't printing because you have indented with tabs in your code instead of spaces.
After fixing that , if it still does not run as expected post your latest code so we can see EXACTLY what you have at this point.
And please enclose the code in the handy cpp tags this time.
And while your waiting for a reply , enjoy reading the guidlines... Thank you.
Last edited by Howard_L : 28-Oct-2009 at 21:40.
  #10  
Old 28-Oct-2009, 21:42
sl02ggp sl02ggp is offline
New Member
 
Join Date: Oct 2009
Posts: 25
sl02ggp is on a distinguished road

Re: How do I add arrays into my program?!?!


here is my latest code:

CPP / C++ / C Code:

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


int main()
{
	char type;																					
	//declare a character type variable
	double total_resistance=0;																	
	//initialize double total_resistance=0
	double resistor_value=0;																	
	//initialize double resistor_value=0
	int number_of_resistors_connected=0;														
	//declare number_of_resistors_connected=0
	

	double *r; //A pointer to hold the address of your dynamically-allocated "array"

	
        cout << "Please input 's' for series OR 'p' for resistors parallel"
            << endl;
        cin >> type;
        cout << "Please enter the number of resistors connected" << endl;
        cin >> number_of_resistors_connected;
        if (!cin || (number_of_resistors_connected <= 0)) {
            cout << "Invalid input. Must be a positive integer." << endl;
           
        }
        r = new double[number_of_resistors_connected];// Storage to hold the resistors' values
        if (type == 's') {
             total_resistance = 0.0;
            for (int i = 0; i < number_of_resistors_connected; i++) {
                cout << "Input value of  series resistor number " << i + 1 << endl;
                cin >> r[i];
                if (!cin) {
                    cout << "Invalid input." << endl;
                    
                }
                total_resistance += r[i];
            }
        }
		if (type == 'p') {
           total_resistance = 0.0;
            for (int i = 0; i < number_of_resistors_connected; i++) {
                cout << "Input value of  parallel resistor number " << i + 1 << endl;
                cin >> r[i];
                if (!cin) {
                    cout << "Invalid input." << endl;
                    
                }
                total_resistance += r[i];
            }
        }
	
	{
	int array [] = {1,2,3,4,5,6};
	int length = sizeof(array) / sizeof(int);

	for(int i = 0; i < length; i++) {
	cout << "Resistor: " << length<< endl;
	}

	if (total_resistance<1000)																	//if total_resistance<1000
	{
		double x=0;																				//declare x=0
		x=total_resistance;																		
		//declare c = total resistance
		cout<<"The total resistance is less than 1000 and its value is "<<x<<" Ohm"<<endl;		//prompt message on screen
	}
	else																						//false statement
	{
		total_resistance=total_resistance/1000;													
		//total_resistance=total_resistance/1000
		cout<<fixed<<showpoint<<setprecision(3);
		cout<<"The total resistance is greater than 1000 and its value is "<<total_resistance<<" kOhm"<<endl;    
		//prompt message on screen
	}


	delete [] r;
	return 0;																					
	//indicate program ended successfully


}
}

 
 

Recent GIDBlogProblems with the Navy (Chiefs) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Equation solver RazoR C Programming Language 3 18-May-2008 10:24
Need Help with input files. Efferus C++ Forum 2 24-Nov-2007 17:19
Two-Tier data dissemination code installation problem nidhibansal1984 Computer Software Forum - Linux 6 16-Sep-2007 11:13
program without arrays dezstylz C++ Forum 3 31-May-2006 09:24

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 21:29.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.